简体   繁体   English

PHP正则表达式查找链接

[英]PHP regex to find links

So im currently working on a regex that's gonna be used on a forum that also had bbcode support. 因此,我目前正在研究正则表达式,该正则表达式将在也具有bbcode支持的论坛上使用。 This regex is suppose to catch all link's beginning with https,http and www and make them to links. 该正则表达式假定捕获以https,http和www开头的所有链接,并使它们成为链接。

Currently it catches all https and http but not the www ones. 当前,它捕获所有https和http,但是不捕获www。 And i can't figure out how to fetch the ones that starts with www. 而且我不知道如何获取以www开头的内容。

Also have in mind, if the link is already inside a bbcode it should not be catched in this regex. 还请记住,如果链接已经在bbcode中,则不应在此正则表达式中捕获该链接。

return preg_replace('/(?<!src=[\"\'])(http(s)?:\/\/(www\.)?[\/a-zA-Z0-9%\?\.\-]*)(?=$|<|\s)/','<a href="$1">$1</a>', $text);

May I suggest trying: 我可以建议尝试:

(?<!src=[\"\'])((http(s)?:\/\/(www\.)?|(www\.))[\/a-zA-Z0-9%\?\.\-]*)(?=$|<|\s)

I believe this should catch the http(s) and www. 我认为这应该可以捕获http(s)和www。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM