get all url in a string

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
lclqt12
Forum Newbie
Posts: 3
Joined: Thu May 06, 2010 11:25 pm

get all url in a string

Post 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
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: get all url in a string

Post 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);
?>
Post Reply