Fyllekanin
Fyllekanin

Reputation: 274

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. This regex is suppose to catch all link's beginning with https,http and www and make them to links.

Currently it catches all https and http but not the www ones. And i can't figure out how to fetch the ones that starts with www.

Also have in mind, if the link is already inside a bbcode it should not be catched in this regex.

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

Upvotes: 0

Views: 47

Answers (1)

Victor Luna
Victor Luna

Reputation: 1814

May I suggest trying:

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

I believe this should catch the http(s) and www.

Upvotes: 2

Related Questions