【发布时间】:2018-09-12 20:40:23
【问题描述】:
我从这个 PHP 字符串开始。
$bodyString = '
another 1 body
reg http://www.regularurl.com/home
secure https://facebook.com/anothergreat.
a subdomain http://info.craig.org/
dynamic; http://www.spring1.com/link.asp?id=100408
www domain; at www.wideweb.com
single no subdomain; simple.com';
需要将所有域、urls 转换为anchor(<a>) 元素。
preg_replace('#[-a-zA-Z0-9@:%_\+.~\#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~\#?&//=]*)?#si', '<a href="$0">$0</a>', $bodyString)
$bodyString 结果:
'another 1 body
reg <ahref="http://www.regularurl.com/home">http://www.regularurl.com/home</a>
secure <a href="https://facebook.com/anothergreat.">https://facebook.com/anothergreat.</a>
a subdomain <a href="http://info.craig.org/">http://info.craig.org/</a>
dynamic; <a href="http://www.spring1.com/link.asp">http://www.spring1.com/link.asp</a>?id=100408
www domain; at <a href="www.wideweb.com">www.wideweb.com</a>
single no subdomain; <a href="simple.com">simple.com</a>';
结果:所有的url、域名都变成<a>除了http://www.spring1.com/link.asp?id=100408
正则表达式中缺少什么来完成这项工作?
【问题讨论】:
-
可能是this will help。
-
我认为如果您可以像问题中那样编写这么多的正则表达式,您可以自己找到解决方案。在从互联网复制粘贴代码之前,请务必从头开始尝试并发布您的研究。