0

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);
2
  • A link always has a protocol leading scheme. You are trying to interpret some string that might be interpreted as a reference, that is a risky thing. Also only few host names of web servers still carry the traditional www. prefix used in the 1980th... Commented Mar 23, 2017 at 16:47
  • The www support is because users on current version of the site are not copying & pasting urls from the site. Instead they know the address and add www. instead of http:// in the url. Commented Mar 23, 2017 at 16:55

1 Answer 1

2

May I suggest trying:

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

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.