0

Possible Duplicate:
Regex to match URL

I know regexp should be able to do this but I can't figure out how to do it. I wan't to be able to search through a string of text and return only the url's. How would you do this in PHP?

Example

hello there everyone visit this link: http://google.com and this one as well http://amazon.com

would return:

http://google.com

http://amazon.com

Any ideas?

Community
  • 1
  • 1
Chris
  • 374
  • 1
  • 5
  • 16

1 Answers1

1

This should work:

$text = "hello there everyone visit this link: http://google.com and this one as well http://amazon.com";
preg_match_all('/\b(([\w-]+:\/\/?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|\/)))/',$text,$matches);
print_r($matches[0]);
Keyne Viana
  • 6,194
  • 2
  • 24
  • 55