Php 使用preg_match解析youtube视频id

Php 使用preg_match解析youtube视频id,php,regex,parsing,youtube,Php,Regex,Parsing,Youtube,我正在尝试使用preg_match解析youtube URL的视频ID。我在这个网站上发现了一个正则表达式,它似乎可以工作 (?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+ (?更好地使用和解析URL和查询字符串: preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^

我正在尝试使用preg_match解析youtube URL的视频ID。我在这个网站上发现了一个正则表达式,它似乎可以工作

(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+
(?更好地使用和解析URL和查询字符串:

 preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+#", $subject, $matches);
使用

preg_匹配(“#(?使用下面的代码

preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]\/)[^&\n]+|(?<=v=)[^&\n]+#", $subject, $matches);

你忘了转义斜杠字符。所以这一个应该可以:

if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/\s]{11})%i', $url, $match)) {
    $video_id = $match[1];
}

preg_match(“#)(?这个正则表达式从我能找到的所有不同URL中获取ID。。。
可能还有更多,但我在任何地方都找不到它们的引用。如果你遇到一个不匹配的,请在URL上留下评论,我会尝试更新正则表达式以匹配你的URL

/**
 *  Check if input string is a valid YouTube URL
 *  and try to extract the YouTube Video ID from it.
 *  @author  Stephan Schmitz <eyecatchup@gmail.com>
 *  @param   $url   string   The string that shall be checked.
 *  @return  mixed           Returns YouTube Video ID, or (boolean) false.
 */        
function parse_yturl($url) 
{
    $pattern = '#^(?:https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch\?v=|/watch\?.+&v=))([\w-]{11})(?:.+)?$#x';
    preg_match($pattern, $url, $matches);
    return (isset($matches[1])) ? $matches[1] : false;
}
下面是这个正则表达式匹配的URL示例:(在给定URL之后可能会有更多内容被忽略)

它也可以在youtube-nocookie.com URL上使用上述相同的选项


它还将从嵌入代码(iframe和object标记)中的URL中提取ID

我在几周前编写的一个PHP类中不得不处理这个问题,最终得到了一个匹配任何类型字符串的正则表达式:有或没有URL方案,有或没有子域,youtube.com URL字符串,youtu.be URL字符串,以及处理所有类型的参数排序。您可以检查它,或者简单地复制并粘贴下面的代码块:

/**
 *  Check if input string is a valid YouTube URL
 *  and try to extract the YouTube Video ID from it.
 *  @author  Stephan Schmitz <eyecatchup@gmail.com>
 *  @param   $url   string   The string that shall be checked.
 *  @return  mixed           Returns YouTube Video ID, or (boolean) false.
 */        
function parse_yturl($url) 
{
    $pattern = '#^(?:https?://)?';    # Optional URL scheme. Either http or https.
    $pattern .= '(?:www\.)?';         #  Optional www subdomain.
    $pattern .= '(?:';                #  Group host alternatives:
    $pattern .=   'youtu\.be/';       #    Either youtu.be,
    $pattern .=   '|youtube\.com';    #    or youtube.com
    $pattern .=   '(?:';              #    Group path alternatives:
    $pattern .=     '/embed/';        #      Either /embed/,
    $pattern .=     '|/v/';           #      or /v/,
    $pattern .=     '|/watch\?v=';    #      or /watch?v=,    
    $pattern .=     '|/watch\?.+&v='; #      or /watch?other_param&v=
    $pattern .=   ')';                #    End path alternatives.
    $pattern .= ')';                  #  End host alternatives.
    $pattern .= '([\w-]{11})';        # 11 characters (Length of Youtube video ids).
    $pattern .= '(?:.+)?$#x';         # Optional other ending URL parameters.
    preg_match($pattern, $url, $matches);
    return (isset($matches[1])) ? $matches[1] : false;
}
/**
*检查输入字符串是否为有效的YouTube URL
*并尝试从中提取YouTube视频ID。
*@作者Stephan Schmitz
*@param$url string应检查的字符串。
*@return mixed返回YouTube视频ID,或(布尔值)false。
*/        
函数parse_yturl($url)
{
$pattern='#^(?:https?:/)?(?:www\)?(?:youtu\.be/| youtube\.com(?:嵌入/|/v/|/watch\?v=|/watch\?。+&v=)([\w-]{11})(?:.+)?$#x';
预匹配($pattern,$url,$matches);
返回(isset($matches[1])?$matches[1]:false;
}
为了解释正则表达式,这里有一个扩展版本:

$yout_url='http://www.youtube.com/watch?v=yxYjeNZvICk&blabla=blabla';

$videoid = preg_replace("#[&\?].+$#", "", preg_replace("#http://(?:www\.)?youtu\.?be(?:\.com)?/(embed/|watch\?v=|\?v=|v/|e/|.+/|watch.*v=|)#i", "", $yout_url));
/**
*检查输入字符串是否为有效的YouTube URL
*并尝试从中提取YouTube视频ID。
*@作者Stephan Schmitz
*@param$url string应检查的字符串。
*@return mixed返回YouTube视频ID,或(布尔值)false。
*/        
函数parse_yturl($url)
{
$pattern='#^(?:https?:/)?'#可选URL方案。http或https。
$pattern.='(?:www\)?';#可选www子域。
$pattern.='(?:'#组主机备选方案:
$pattern.=“youtu\.be/”;#要么youtu.be,
$pattern.='| youtube\.com'#或youtube.com
$pattern.='(?:'#组路径备选方案:
$pattern.='/embed/'#或/embed/,
$pattern.='|/v/'#或/v/,
$pattern.='|/watch\?v='|#或/watch?v=,,
$pattern.='|/watch\?.+&v='|#或/watch?其他参数&v=
$pattern.=')'#结束路径选项。
$pattern.=')'#终端主机替代方案。
$pattern.='([\w-]{11});#11个字符(Youtube视频ID的长度)。
$pattern.='(?:.+)?$#x';#可选其他结束URL参数。
预匹配($pattern,$url,$matches);
返回(isset($matches[1])?$matches[1]:false;
}
这对我很有效

'#\[yt\]https?://(?:[0-9A-Z-]+\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch\?v=|/ytscreeningroom\?v=|/feeds/api/videos/|/user\S*[^\w\-\s]|\S*[^\w\-\s]))([\w\-]{11})[?=#&+%\w-]*(t=(\d+))?\[/yt\]#Uim'

我从引导答案中完善了regex。它还从所有不同的URL获取ID,但是更准确地说

if(preg#u match('%(?:youtube(?:nocookie)?\.com/(?:[\w\-?&!#=,;]+/[\w\-?&!#=/,;]+/(?:v | e(?:mbed)?)/|[\w\-?&!*[?&]v=;youtu be/)([\w]{11(?:[\w-],[\w-,]i-]{
$video_id=$match[1];
}
此外,它还可以正确处理超过11个字符的错误ID


http://www.youtube.com/watch?v=0zM3nApSvMgDw3qlxF

解析BBcode()的开始参数

示例:
[yt]http://www.youtube.com/watch?v=G059ou-7wmo#t=58[/yt]

PHP正则表达式:

'<iframe id="ytplayer" type="text/html" width="639" height="360" src="http://www.youtube.com/embed/$1?rel=0&vq=hd1080&start=$3" frameborder="0" allowfullscreen></iframe>'
preg_match("(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+", $subject, $matches);
替换:

'<iframe id="ytplayer" type="text/html" width="639" height="360" src="http://www.youtube.com/embed/$1?rel=0&vq=hd1080&start=$3" frameborder="0" allowfullscreen></iframe>'
preg_match("(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+", $subject, $matches);
“”

我没有看到任何人直接解决PHP错误,因此我将尝试解释

“未知修饰符“[]”错误的原因是您忘了用分隔符包装正则表达式。PHP只将第一个字符作为分隔符,只要它是非字母数字、非空白ASCII字符。因此,在正则表达式中:


preg_匹配(“(?这是可行的,但我刚刚用URL测试了它,但它失败了,你能修改它以使用这种格式吗?我接受这个答案,因为它确实回答了我原来的问题。我现在正在修改它以使用URL@Webbo:
parse\u URL
返回一个URL部分数组,因此URL路径也在其中。你需要执行以下操作:进一步区分URL的类型。我宁愿使用正则表达式在oneit中完成所有工作!我已经尝试过了?给我任何不符合此标准的示例?其他相关帖子
http://stackoverflow.com/questions/2164040/grab-the-youtube-video-
id with jquery match`在您的RegexBuddy中,您选择了Java作为语言。还有一个“使用”选项卡,您可以单击该选项卡,它将为您提供正确的转义代码,以用于多种不同的情况。请参见:相关:因为另一个问题有一个最好的答案,解释得很好。@Toto如果您看到最新的注释,在某些情况下,它也无法匹配-因此我使用的不是更好的答案我总是在视频ID中得到结尾/iframe>。你能给出一个链接到一个pastebin示例,说明你到底在做什么吗?或者在这里创建一个问题,然后在这里链接到它吗?再一次……你有代码示例吗?你是否正确地使用了它?我刚刚用你的URL测试了它,它返回了一个数组,并且在
$match[1]中
waspreg_match("(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+", $subject, $matches);