我需要在一些尚未链接的内容中添加指向 url 的链接。此内容来自 Joomla 内部的许多地方,有时是 html,有时是文本。我需要一个单行正则表达式来做到这一点。
考虑内容:
some text then url: <a href="http://j25.localhost/something">Link</a> and some
other text<br />url:http://j25.localhost/something2 and some other text...
我的正则表达式是这样的:
$content = preg_replace('@(?:[^"])(https?://?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@i', '<a href="$1">$1</a>', $content);
它几乎可以工作(它跳过第一个并匹配第二个)但是在第二个上它在“http ....”之前包含字符“:”并且替换后我的“:”消失了,导致:“ <br />url<a ...
“而不是<br />url:<a ...
”
该怎么办?