【发布时间】:2014-08-20 17:21:02
【问题描述】:
<?php
// I have a string, something like this:
$string = '
Lorep ipsum <a href="http://www.example.com">example</a> lorem ipsum
lorem ipsum http://www.example.com/index.php?param1=val¶m2=val lorem ipsum
';
// I need to do some magick with preg_replace and get string like this:
$string = '
Lorep ipsum <a href="http://www.example.com" target="_blank">example</a> lorem ipsum
lorem ipsum <a href="http://www.example.com/index.php?param1=val¶m2=val" target="_blank">http://www.example.com/index.php?param1=val¶m2=val</a> lorem ipsum
';
?>
所以基本上,我想链接未包含在 中的文本中的 URL,并将 target="_blank" 添加到那些包含的文本中。
谁能帮我解决这个问题?
【问题讨论】:
-
这可能不是一个适合正则表达式的任务——我建议使用标准的字符串方法以及库函数来识别 URL,因为正则表达式会完全正确地做到这一点。