Grab URLs from a String in PHP


Found this little Regex pattern which is an improved version of Daring Fireball’s (found here). Throw whatever string of various mixed up URLs you can at it, and this will return an array of them.

[code lang="php"]
$regexp = '/\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:(?:[^\s()<>.]+[.]?)+|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))/i';

preg_match_all($regexp, $message, $matches);

return $matches[0];
[/code]