如何使用preg_match或preg_replace将目标空白添加到锚标记中以在新窗中打开


how to add target blank to anchor tag to open in new window using preg_match or preg_replace

如何在使用preg_matchpreg_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];

不需要preg match/replace使用str_replace()

str_replace('<a', '<a target="_blank" ', $str);

你可以检查preg_match是否已经有目标属性

if(preg_match('/<a.*?target=[^>]*?>/', $str))

并决定要做什么,如果你想替换这个标签,那么使用preg_replace或str_replace。你也可以决定,如果它有这个标签,那么不做替换。由你决定。

$str为待更换/检查字符串。

如果要用blank替换target,可以使用

preg_replace('/<a.*?target="([^"]?)"[^>]*?>/', 'blank', $str)