PHP:从 YouTube URL 中提取视频 ID
我做错了什么吗? 我需要 youtube 代码,但它不会返回真正的值。
if(preg_match_all("http:\/\/www\.youtube\.com\/v\/(.*)(.*)", $row->n_texto, $matches){
$code = $image_to_thumb .= "http://i1.ytimg.com/vi/".$matches[1][0]."/0.jpg";
}
编辑 - ircmaxell 根据评论,文本中的链接结构为:
http://www.youtube.com/v/plMvAh10HVg%26hl=en%26fs=1%26rel=0
更新
问题是:我的代码返回这样的链接:
http://www.youtube.com/v/plMvAh10HVg%26hl=en%26fs=1%26rel=0
我可以用以下命令停止它吗正则表达式之前出现%26hl=en%26fs=1%26rel=0
?
Do I do something wrong?
I need the youtube code, but it doesn't return the real value.
if(preg_match_all("http:\/\/www\.youtube\.com\/v\/(.*)(.*)", $row->n_texto, $matches){
$code = $image_to_thumb .= "http://i1.ytimg.com/vi/".$matches[1][0]."/0.jpg";
}
Edit - ircmaxell Based on the comment, the link structure in the text is:
http:// www.youtube.com/v/plMvAh10HVg%26hl=en%26fs=1%26rel=0
Update
The problem is: my code return a link like this:
http://www.youtube.com/v/plMvAh10HVg%26hl=en%26fs=1%26rel=0
Can I stop it with regexp before appear %26hl=en%26fs=1%26rel=0
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据 http://www.regexlib.com/REDetails.aspx?regexp_id=2569
according to http://www.regexlib.com/REDetails.aspx?regexp_id=2569
你的正则表达式不正确。它有很多问题。现在,就您想要的而言,尝试一下:
现在,至于为什么,让我们看看正则表达式:
您正在寻找一个以
http://
开头的字符串,在 ( 之后有任何内容www.
、ww2.
或什么都没有)。您正在寻找
/v/
作为 URL 的开头。您正在寻找其他一切,直到另一个
/
、查询字符串 (?
) 或锚点 (#
)。所以这应该与您要查找的 ID 相匹配。所以,
如果你想找到全部:
Your regex is not correct. There are more than a few things wrong with it. Now, as far as what you want, try this:
Now, as for why, let's look at the regex:
You're looking for a string that starts with
http://
, has anything after (www.
,ww2.
, or nothing).You're looking for
/v/
as the start of the URL.You're looking for everything else UP TO another
/
, a query string (?
) or a anchor (#
). So that should match the ID you're looking for.So, it would be
If you wanted to find all:
提供的链接在 www.youtube.com 中的第一个 w 之前有一个空格,您需要的代码是:
另外,您拥有的 url 已编码,您可能需要在使用它之前使用 urldecode($row->n_texto) 。
the link provided has a space before the 1st w in www.youtube.com, the code you need is :
also, the url you have is encoded, you may want to use urldecode($row->n_texto) before using it.