使用 PHP 从 html 代码中获取 Youtube 视频 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1773822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Youtube Video ID from html code with PHP
提问by asumaran
I want to get all only youtube video ID from html code
我想从 html 代码中获取所有仅 youtube 视频 ID
look the (or multiple) object/embed code for youtube video
查看 youtube 视频的(或多个)对象/嵌入代码
// html from database
// 来自数据库的 html
<p>loremm ipsum dolor sit amet enot
<a href="link" attribute=""blah blah blah">anchor link</a>
</p>
<object width="425" height="344">
<param name="movie" value="http://www.youtube.com/v/Ou5eVl5eqtg&hl=es_ES&fs=1&"></param>
<param name="allowFullScreen" value="true"></param>
<param name="allowscriptaccess" value="always"></param>
<embed src="http://www.youtube.com/v/Ou5eVl5eqtg&hl=es_ES&fs=1&"
type="application/x-shockwave-flash"
allowscriptaccess="always"
allowfullscreen="true"
width="425"
height="344">
</embed>
</object>
<image src="path/to/image.ext" >
<p>lorem ipsum dolor sit amet... blah</p>
<p>lorem ipsum dolor sit amet... blah</p>
<object width="425" height="344">
<param name="movie" value="http://www.youtube.com/v/Ou5eVl5eqtg&hl=es_ES&fs=1&"></param>
<param name="allowFullScreen" value="true"></param>
<param name="allowscriptaccess" value="always"></param>
<embed src="http://www.youtube.com/v/Ou5eVl5eqtg&hl=es_ES&fs=1&"
type="application/x-shockwave-flash"
allowscriptaccess="always"
allowfullscreen="true"
width="425"
height="344">
</embed>
</object>
<p>blah</p>
blah<br/>
blah<br/>
blah<br/>
回答by Jim Mischel
There are generally two formats for YouTube video urls:
YouTube 视频网址通常有两种格式:
http://www.youtube.com/v/[videoid]
http://www.youtube.com/watch?v=[videoid]
The "www.youtube.com" can be replaced by "www.youtube.co.uk", or other country codes, but as far as I've been able to determine, the video ids are the same regardless of the domain name.
“www.youtube.com”可以替换为“www.youtube.co.uk”或其他国家/地区代码,但据我所知,无论域名如何,视频 ID 都是相同的.
The video id is an 11-character string that uses base-64 encoding.
视频 ID 是一个 11 个字符的字符串,使用 base-64 编码。
Assuming you have code that will parse urls from an HTML document, you can determine if it's a YouTube video url and get the video id by using this regex (written in C#, but should be easily converted to php or anything else):
假设您有从 HTML 文档解析 url 的代码,您可以确定它是否是 YouTube 视频 url 并使用此正则表达式获取视频 ID(用 C# 编写,但应该很容易转换为 php 或其他任何内容):
"^http://(?<domain>([^./]+\.)*youtube\.com)(/v/|/watch\?v=)(?<videoId>[A-Za-z0-9_-]{11})"
This particular regex is specific to youtube.com. Making it understand all the different country codes (youtube.co.uk, youtube.pl, youtube.it, etc.) is somewhat more involved.
这个特定的正则表达式特定于 youtube.com。让它理解所有不同的国家/地区代码(youtube.co.uk、youtube.pl、youtube.it 等)有点复杂。
回答by Sphere
Actually, to completely capture all options, I found that WebFlakeStudio's solution is the best, with the following addition, to capture all 3 forms of *cough*client stupidity*cough*
实际上,要完全捕获所有选项,我发现 WebFlakeStudio 的解决方案是最好的,加上以下内容,可以捕获所有 3 种形式的 *咳嗽*客户愚蠢*咳嗽*
(PHP)
(PHP)
preg_match('#(\.be/|/embed/|/v/|/watch\?v=)([A-Za-z0-9_-]{5,11})#', $YoutubeCode, $matches);
if(isset($matches[2]) && $matches[2] != ''){
$YoutubeCode = $matches[2];
}
I added the /embed, this should capture all. The Object, the URL and the Embed-option.
我添加了/嵌入,这应该捕获所有。对象、URL 和嵌入选项。
回答by Frank Farmer
Brazenly stolen from htmlpurifier's youtube plugin:
明目张胆地从 htmlpurifier 的 youtube 插件中窃取:
preg_match('#<object[^>]+>.+?http://www.youtube.com/v/([A-Za-z0-9\-_]+).+?</object>#s', $markup, $matches);
var_dump($matches[1]);
回答by Roman Snitko
If you want to get embed link for youtube video, you can use the following code snippet:
如果您想获得 youtube 视频的嵌入链接,您可以使用以下代码片段:
$youtubeRegexp = "#(/v/|/watch\?v=)([A-Za-z0-9_-]{5,11})#";
$embedUrl = preg_replace($youtubeRegexp, '/embed/', $videoUrl);
For the current moment embed code is:
目前嵌入代码是:
<iframe width="{width}" height="{height}" src="{embed_url}" frameborder="0" allowfullscreen></iframe>
Note: $videoUrl should be set to the original url prior to running this expression.
注意:在运行此表达式之前,应将 $videoUrl 设置为原始 url。
回答by Galen
I might get scolded for using a regex to parse html but given the circumstances maybe it's the best way to do it?
我可能会因为使用正则表达式解析 html 而受到责骂,但鉴于这种情况,这可能是最好的方法吗?
preg_match('~/v/([0-9a-z_]+)~i', $code, $matches);
echo $matches[1];
assuming the valid characters for a youtube video id are 0-9a-z_
假设 youtube 视频 ID 的有效字符是 0-9a-z_

