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!

2

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

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

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

2 Comments

For some reason php preg_match said i couldnt use \ as delimiter :(
Try just // instead of \/\/, some implementations don't require you to escape forward slashes.
0

You'll want a regex pattern like the following:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.