我有一个字符串,我需要将任何网址替换为<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';
答案 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'