【问题标题】:Getting first video url (youtube,vimeo or dailymotion) out of string containing more than one video links in PHP从包含多个 PHP 视频链接的字符串中获取第一个视频 url(youtube、vimeo 或 dailymotion)
【发布时间】:2013-07-21 04:25:43
【问题描述】:

我需要包含多个链接的字符串中的第一个网址(视频网址可能是 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>"

在这种情况下,我正在尝试获取 url http://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

谢谢

【问题讨论】:

  • 你认为\S*是什么意思?
  • 我认为这意味着所有包含
    的字符串。如果 youtube 网址像 youtube.com/watch?v=wE7AQY5Xk9w
    。不会发生此问题(ID 后有空格)。
  • “我认为这意味着所有字符串”---不猜测而是检查文档怎么样?

标签: php regex


【解决方案1】:

不确定您是否从上面的 cmets 中解决了这个问题...但是,您的正则表达式似乎已经走了很长一段路。

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

与以下网址匹配:

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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-22
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 2011-10-12
    相关资源
    最近更新 更多