如何替换所有文本链接并使用 php 添加 <a href> 标签?

How to replace all textlink and add <a href> tag using php?

我想添加 <a href=""> 到我错过的所有文本链接 添加 <a href=""> 当我提交 post.

例如:

<a href"http://google.com">google.com</a> and https://paypal.com

我只想为我错过的文本 https://paypal.com 添加 <a href=""> 添加 <a href="">

谢谢!

使用preg_replace函数和负后向断言的解决方案:

$str = '<a href="http://google.com">google.com</a> and https://paypal.com';
$result = preg_replace("/(?<![=\">])https?:\/\/([^\s]+)/", '<a href=""></a>', $str);

print_r($result);

输出(作为源代码):

<a href="http://google.com">google.com</a> and <a href="paypal.com">paypal.com</a>

(?<!=\") - 负向后断言,确保匹配的 URL 之前没有属性赋值 ="