tags:

views:

21

answers:

1

Hi all still struggling with regex :) i have this code:

$link = '/\bhttp:\/\/.+\..+[\/\w]?\b/i';
$match = array();
if (preg_match($link, 'http://bit.ly/hghjK6 bla bla',&$match))
{
    echo 'match'.'</br>';
    print_r($match);
}

as i'm trying to extract only the URL from the string i cant isolate it and the value inserted into $match always contain the "bla bla" taht follows the URL in my string what is the regex needed to only match the URL and nothing else that comes after? Thanks

+1  A: 
$r = '`(\s|\A)(http|https|ftp|ftps)://(.*?)(\s|\n|[,.?!](\s|\n)|$)`ism'; // regex to match a URL

$i = preg_match_all($r, $str, $m, PREG_SET_ORDER);
if($i){
  foreach($m as $set){
    // each $set in $m is a group of match
    // complete URL is
    echo $set[2].'://'.$set[3];
  } 
}

this matches ALL the URL in a text.

thephpdeveloper
sorry this didnt work for me are you sure about that regex?
Yaniv
sorry about that Yaniv. fixed.
thephpdeveloper
don't be sorry! I'm the asshole :) i forgot to change the $str to my string (the curly braces is just a typing error)now work like a charm :)thanks for that!
Yaniv
no worries, i really found bug in my own writing. i missed out an open brace, missed out a ` at the start of the regex string and so on. cheerios for working code!
thephpdeveloper