【发布时间】: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 <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 后有空格)。 -
“我认为这意味着所有字符串”---不猜测而是检查文档怎么样?