Reputation: 180
I need the first url out of the string which contains more than one links ( videos url may be youtube, vimeo or dailymotion)
This is my content which is stored in $content
variable.
"Hello this is a test video<br>http://www.youtube.com/watch?v=wE7AQY5Xk9w<br>This is another test <br>http://www.youtube.com/watch?v=GqcqapoFy-w<br>"
In this case, I am trying to get the url http://www.youtube.com/watch?v=wE7AQY5Xk9w
I have used this code to get the first link.
$pattern = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
preg_match($pattern, $content, $matches);
print_r($matches);
But I am getting the result
array(
(int) 0 => 'http://www.youtube.com/watch?v=wE7AQY5Xk9w<br>This',
(int) 1 => 'http',
(int) 2 => '/watch?v=wE7AQY5Xk9w<br>This'
)
Is there anything I am missing in regex pattern.
I am using PHP 5.3.13
Thanks
Upvotes: 2
Views: 1400
Reputation: 6148
Not sure if you worked this one out from the comments above or not... But, it seems like you're going the long way around with your regex.
You could just use: /((http|https|ftp|ftps)[:=#\w\.\/\?\-]+)/
Which would match the following URLs:
http://www.dailymotion.com/video/x124dv4_8-month-old-deaf-baby-hears-for-the-first-time_fun#.Ueu0YuEju1E
http://www.youtube.com/watch?v=wE7AQY5Xk9w
http://vimeo.com/channels/staffpicks/67576646
http://www.youtube.com/watch?v=GqcqapoFy-w
In this text:
Hello http://www.dailymotion.com/video/x124dv4_8-month-old-deaf-baby-hears-for-the-first-time_fun#.Ueu0YuEju1E this is a test video<br>http://www.youtube.com/watch?v=wE7AQY5Xk9w<br>This is another http://vimeo.com/channels/staffpicks/67576646 test <br>http://www.youtube.com/watch?v=GqcqapoFy-w<br>
Upvotes: 1