PHP正则表达式查找修改当前匹配项

时间:2013-11-22 20:20:27

标签: php html regex

我有一个字符串,我需要将任何网址替换为<a> html标记

$str='Lorem Ipsum is simply dummy text of http://www.lipsum.com/ the printing';

$outputString=
'Lorem Ipsum is simply dummy text of <a href="http://www.lipsum.com">http://www.lipsum.com/</a> the printing';

1 个答案:

答案 0 :(得分:1)

$str='Lorem Ipsum is simply dummy text of http://www.lipsum.com/ the printing';
$pattern='/\b((https?:\/\/|www.)[\w.\/-?=&#]+)(?=\s)/i';
$replace='<a href="$1">$1</a>';
echo preg_replace($pattern, $replace, $str);

<强>输出:

Lorem Ipsum is simply dummy text of <a href="http://www.lipsum.com/">http://www.lipsum.com/</a> the printing'

Working on RegExr