从PHP中包含多个视频链接的字符串中获取第一个视频url(youtube、vimeo或dailymotion)


Getting first video url (youtube,vimeo or dailymotion) out of string containing more than one video links in PHP

我需要包含多个链接的字符串中的第一个url(视频url可能是youtube、vimeo或dailymotion)

这是我的内容,存储在$content变量中。

"Hello this is a test video<br>http://www.youtube.com/watch?v=wE7AQY5Xk9w<br>This is another test&nbsp;<br>http://www.youtube.com/watch?v=GqcqapoFy-w<br>"

在这种情况下,我正在尝试获取urlhttp://www.youtube.com/watch?v=wE7AQY5Xk9w

我已经用这个代码获得了第一个链接。

$pattern = "/(http|https|ftp|ftps)':'/'/[a-zA-Z0-9'-'.]+'.[a-zA-Z]{2,3}('/'S*)?/";
preg_match($pattern, $content, $matches);
print_r($matches);

但我得到了的结果

array(
    (int) 0 => 'http://www.youtube.com/watch?v=wE7AQY5Xk9w<br>This',
    (int) 1 => 'http',
    (int) 2 => '/watch?v=wE7AQY5Xk9w<br>This'
)

正则表达式模式中是否缺少任何内容。

我使用的是PHP 5.3.13

感谢

不确定你是否从上面的评论中找到了这个。。。但是,您的正则表达式似乎要走很长的路。

您可以使用:/((http|https|ftp|ftps)[:=#'w'.'/'?'-]+)/

将匹配以下URL:

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

在此文本中:

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&nbsp;<br>http://www.youtube.com/watch?v=GqcqapoFy-w<br>