如何使用preg_match或preg_replace将目标空白添加到锚定标记以在新窗口中打开
$contentss = file_get_contents("http://www.ncbi.nlm.nih.gov/pubmed?LinkName=pubmed_pubmed&from_uid=18032633" );
preg_match('/<div class="rprt">(.*)<\/div>/',$contentss,$matches);
$patterns = '/\/pubmed/';
$replacements = 'http://ncbi.nlm.nih.gov/pubmed';
$getreplacements = ( preg_replace($patterns, $replacements, $matches));
echo $getreplacements[0];发布于 2013-06-07 15:45:43
不需要预置匹配/替换使用str_replace()
str_replace('<a', '<a target="_blank" ', $str);如果已经有了目标属性,可以通过preg_match进行检查
if(preg_match('/<a.*?target=[^>]*?>/', $str))然后决定该怎么做,如果你想替换这个标签,那就使用preg_replace或str_replace。你也可以决定,如果它有这个标签,那么就不做替换。由你决定。
$str是要替换/检查的字符串。
要在目标存在时用空白替换目标,可以使用
preg_replace('/<a.*?target="([^"]?)"[^>]*?>/', 'blank', $str)https://stackoverflow.com/questions/16978775
复制相似问题