Page 1 of 1

get all url in a string

Posted: Thu May 06, 2010 11:32 pm
by lclqt12
hi all,

I have a string : "links : http://aa.link.com link 2 http://fea.link.com http://feafd.nlin.com all link"
How could i get all url in above string ?

the expected result will be an array with :
[0] : http://aa.link.com link
[1] : http://fea.link.com
[2] : http://feafd.nlin.com

Re: get all url in a string

Posted: Fri May 07, 2010 12:53 am
by Apollo
Welcome to the wonderful world of Regular Expressions:

Code: Select all

<?php
$s = "links : http://aa.link.com link 2 http://fea.link.com http://feafd.nlin.com all link";
while (preg_match('#(https?://[^\s]+)(.*)$#i',$s,$m)) { $links[] = $m[1]; $s = $m[2]; }
print_r($links);
?>