I’ve found a page with a list of regular expressions that test for valid url’s.
http://mathiasbynens.be/demo/url-regex
The one from Diego Pernini seems the be the best choice.
https://gist.github.com/729294
Now I just can’t get this regex to work with finding a url in a string.
I want to isolate the url in this string:
$string = 'Really digging new the twitter design! http://t.co/71dEuIY8';
This regex gives me 0
preg_match_all('%^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@|\d{1,3}(?:\.\d{1,3}){3}|(?:(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)(?:\.(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)*(?:\.[a-z\x{00a1}-\x{ffff}]{2,6}))(?::\d+)?(?:[^\s]*)?$%iu', $string, $match);
It does match a valid url when I use a string which only contains a url:
$string = 'http://t.co/71dEuIY8';
How do I tweak this regex to isolate a valid url, or urls, in a string with more than just a url?
Your problem is the ^ at the start and the $ at the end.
These mean the link must start and end this way. That is why it matches the isolated link but not in the string.