[英]How to replace all textlink and add <a href> tag using php?
i want to add <a href="">
to all textlink which i missed add <a href="">
when i submit post. 我想将
<a href="">
添加到我错过的所有textlink中添加<a href="">
当我提交帖子时。
Ex: 例如:
<a href"http://google.com">google.com</a> and https://paypal.com
And i just want to add <a href="">
for text https://paypal.com
which i missed add <a href="">
我只想添加
<a href="">
文本https://paypal.com
我错过了添加<a href="">
Thanks! 谢谢!
The solution using preg_replace
function and negative lookbehind assertion : 使用
preg_replace
函数和负向lookbehind断言的解决方案:
$str = '<a href="http://google.com">google.com</a> and https://paypal.com';
$result = preg_replace("/(?<![=\">])https?:\/\/([^\s]+)/", '<a href="$1">$1</a>', $str);
print_r($result);
The output(as source code): 输出(作为源代码):
<a href="http://google.com">google.com</a> and <a href="paypal.com">paypal.com</a>
(?<!=\\")
- negative lookbehind assertion, ensures that matched URL is not preceded by attribute assignment ="
(?<!=\\")
- 负面的lookbehind断言,确保匹配的URL前面没有属性赋值="
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.