1

Imagine I have a string - something like this:

This is some really cool text about http://google.com/ and it also contains some urls like http://apple.com/ and its very nice! This is too long and I need to do some magic stuff to fix this very big problem. Oh no.

As you can see there are two URLs in the string and somehow, assuming I need some kind of REGEX I need to get an array of those URL's so I can manipulate them. Something like this...

Array()

- [0] = 'http://google.com/'
- [1] = 'http://apple.com/'

All help appreciated :) Thanks!

tarnfeld
  • 25,992
  • 41
  • 111
  • 146
  • possible duplicate of [extract urls from text in PHP](http://stackoverflow.com/questions/910912/extract-urls-from-text-in-php) – Gordon Jul 13 '10 at 21:30
  • And since you didnt specify a language, here is a couple others: http://stackoverflow.com/search?q=extract+urls+out+of+text – Gordon Jul 13 '10 at 21:36

3 Answers3

2
https?:\/\/[^\s]+

Find something that starts with http:// or https://, then pull characters until we find whitespace.

Donald Miner
  • 38,889
  • 8
  • 95
  • 118
2

I think this should also work.

$string = 'ajskldfj http://google.ca jslfkjals s http://www.apple.com jalksf';
$pattern = '/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/';
preg_match_all($pattern, $string, $matches);
Dylan
  • 859
  • 1
  • 8
  • 12
0

You'll want a regex pattern like the following:

'https?:\/\/(\w*?.)?(?P<domain>\w+).(\w+)/'
Josiah
  • 4,754
  • 1
  • 20
  • 19