views:

47

answers:

3

Hi,

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!

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

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

orangeoctopus
For some reason php preg_match said i couldnt use \ as delimiter :(
tarnfeld
Try just // instead of \/\/, some implementations don't require you to escape forward slashes.
orangeoctopus
hmm still not working :/
tarnfeld
+2  A: 

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
A: 

You'll want a regex pattern like the following:

'https?:\/\/(\w*?.)?(?P<domain>\w+).(\w+)/'
Josiah