0

我有这样的文本Too early to post? http://www.somewebsite.com/article/226973我想解析文本中的超链接并使文本看起来像所以Too early to post? <a href="http://www.entrepreneur.com/article/226973" target="_blank">http://www.entrepreneur.com/article/226973</a>我想这样做但我不知道从哪里开始或使用什么正则表达式。

4

1 回答 1

1
$s = 'Too early to post? https://www.somewebsite.com/article/226973'; 
$parsed = preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a href="$1" target="_blank">$1</a>', $s); 
echo $parsed;
于 2013-07-13T18:45:02.683 回答