1

I want to get url from the given data.

e.g. I have the data in a variable

$data = "is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, http://www.youtube.com/watch?v=mm78xlsADgc when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but";

Now I want to get this URL (http://www.youtube.com/watch?v=mm78xlsADgc) from the given variable $data. Please let me know how can I do this?

hakre
  • 193,403
  • 52
  • 435
  • 836
  • did you even search in this site ? – Aysennoussi Apr 03 '14 at 09:34
  • Yes i have searched on the site but i didn't get solution that's why i posted this question. – Sunil Jindal Apr 03 '14 at 09:35
  • You could try looking for URL validation regexes, e.g. http://stackoverflow.com/questions/206059/php-validation-regex-for-url/207627#207627 – cmbuckley Apr 03 '14 at 09:36
  • Does this return the url from the $data variable? I think it will validate that string is a url or not. am I right? – Sunil Jindal Apr 03 '14 at 09:41
  • I managed to link to the wrong answer in that question. My intention was to link to the accepted answer, which uses a regex. You won't be able to use `filter_var` in your case, but you can pattern-match for URLs. – cmbuckley Apr 03 '14 at 09:52

6 Answers6

0

You should filter_var() to do this.

var_dump(filter_var('http://www.youtube.com/watch?v=', FILTER_VALIDATE_URL));
Sulthan Allaudeen
  • 11,330
  • 12
  • 48
  • 63
0

Using the pattern from this answer and your $data above:

preg_match_all(
    '#((?:http|https|ftp)://(?:\S*?\.\S*?))(?:\s|\;|\)|\]|\[|\{|\}|,|"|\'|:|\<|$|\.\s)#i',
    $data,
    $matches
);
var_dump($matches[1]);

See it in action on Ideone.

Depending on the type of URLs you want to match, you might want to play around with the actual pattern in use, but the concept is the same.

Community
  • 1
  • 1
cmbuckley
  • 40,217
  • 9
  • 77
  • 91
0

Try This

$data = "is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, http://www.youtube.com/watch?v=mm78xlsADgc when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but";
$array1 = explode(" ", $data);
$searchword = 'http';
$matches = array();
foreach($array1 as $k=>$v) {
    if(preg_match("/\b$searchword\b/i", $v)) {
       echo $matches[$k] = $v;
    }
}
Ragavendran Ramesh
  • 376
  • 1
  • 7
  • 23
0
<?php  
$data = "is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, http://www.youtube.com/watch?v=mm78xlsADgc when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but";

preg_match('/(https?:.*?)\s+/si', $data, $url);

echo $url[0];
?>

DEMO

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

How about:

preg_match_all(
    '#(https?://(?:\S+))#i',
    $data,
    $matches
);

\S stands for any character that is not a space.

Toto
  • 89,455
  • 62
  • 89
  • 125
0

Use this code, it works well for me

$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

// The Text you want to filter for urls
$text = "is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, http://www.youtube.com/watch?v=mm78xlsADgc when an unknown printer took a galley of type and scrambled it to make a type   specimen book. It has survived not only five centuries, but";

// Check if there is a url in the text
if(preg_match($reg_exUrl, $text, $url)) {


       return preg_replace($reg_exUrl, $url[0], $text);

} 

}
sami
  • 49
  • 13