我一直在努力让PHP preg_match
与feature=endscreen
youtube网址一起使用,但没有运气。任何建议。
(preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match))
示例输入= http://www.youtube.com/watch?feature=endscreen&NR=1&v=uOHvZjiDANg 必需输出= uOHvZjiDANg
答案 0 :(得分:1)
这是我测试过的代码试试吧
<?php
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;
}
echo parse_yturl('http://www.youtube.com/watch?feature=endscreen&NR=1&v=uOHvZjiDANg');
?>
output = uOHvZjiDANg
来源:https://github.com/eyecatchup/php-yt_downloader/blob/master/youtube-dl.class.php#L406
答案 1 :(得分:0)
试试这个:
if(preg_match('/youtube\.com\/(v\/|watch\?v=)([\w\-]+)/', $url, $match)){; $vid=$match[2];
}
print "$vid";
答案 2 :(得分:0)
我建议不要尝试复杂的正则表达式: