0

可能重复:
用于从任何 YouTube URL 获取 YouTube 视频 ID 的 RegEx 模式

我已将 youtube url 存储在数据库中。我只想从 youtube url 中获取 youtube id。我只想从下面的 url 中提取 id(6FjfewWAGdE)。

$youtubeVal=http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage
4

4 回答 4

1

你可以这样做

var regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed)([^#\&\?]*).*/;
var match = url.match(regExp);
if (match&&match[2].length==11){
    return match[2];
} 
于 2012-10-24T07:11:15.447 回答
0

Youtube URL 有多种格式(使用 embed/<id> 或使用 watch?v=<id>)。找到您想要理解的 URL 类型并构建正则表达式以正确提取它们。

于 2012-10-24T07:14:42.540 回答
0

这是PHP中一个有效的非正则表达式解决方案:

function GetYoutubeID($url) {
    $temp = parse_url($url);

    if(isset($temp['query'])) {
        parse_str($temp['query'], $temp2);
        if(isset($temp2['v'])) {
            return $temp2['v'];
        }
    }

    if(isset($temp['path'])) {
        $temp2 = explode("/", $temp['path']);
        foreach($temp2 as $value) {
            if(strlen($value) == 11 || strlen($value) == 10) {
                return $value;
            }
        }
    }

    return "no ID?";
}

echo $youtubeID = GetYoutubeID('http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage') . "\n";
echo $youtubeID = GetYoutubeID('https://www.youtube.com/embed/6FjfewWAGdE') . "\n";
echo $youtubeID = GetYoutubeID('https://youtube.com/embed/6FjfewWAGdE') . "\n";
echo $youtubeID = GetYoutubeID('http://www.youtube.com/watch?v=6FjfewWAGdE') . "\n";
echo $youtubeID = GetYoutubeID('http://www.youtube.com/watch?v=6FjfewWAGdE&feature=player_detailpage') . "\n";
echo $youtubeID = GetYoutubeID('www.youtu.be/6FjfewWAGdE') . "\n";
echo $youtubeID = GetYoutubeID('youtu.be/6FjfewWAGdE') . "\n";
echo $youtubeID = GetYoutubeID('youtube.com/watch?v=6FjfewWAGdE') . "\n";
echo $youtubeID = GetYoutubeID('https://www.youtube.com/watch?v=6FjfewWAGdE&feature=youtu.be') . "\n";

将函数的全部内容替换为以下内容,以使用更好看的解决方案(正则表达式):

preg_match('/^(http:\/\/|https:\/\/|.*?)(www.|.*?)(youtube.com|youtu.be)\/(embed\/|watch\?v=|.*?)(.*?)(\?|\&|$)/is', $url, $matches);
if(isset($matches[5])) {
    return $matches[5];
}

return "no ID?"

正则表达式解决方案也适用于Javascript

function GetYoutubeID(url) {
    var regExp = /^(http:\/\/|https:\/\/|.*?)(www.|.*?)(youtube.com|youtu.be)\/(embed\/|watch\?v=|.*?)(.*?)(\?|\&|$)/;
    var match = url.match(regExp);
    if (match){
        return match[5];
    }

    return "no ID?";
}

console.log(GetYoutubeID('http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage'));
console.log(GetYoutubeID('http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage'));
console.log(GetYoutubeID('https://www.youtube.com/embed/6FjfewWAGdE'));
console.log(GetYoutubeID('https://youtube.com/embed/6FjfewWAGdE'));
console.log(GetYoutubeID('http://www.youtube.com/watch?v=6FjfewWAGdE'));
console.log(GetYoutubeID('http://www.youtube.com/watch?v=6FjfewWAGdE&feature=player_detailpage'));
console.log(GetYoutubeID('www.youtu.be/6FjfewWAGdE'));
console.log(GetYoutubeID('youtu.be/6FjfewWAGdE'));
console.log(GetYoutubeID('youtube.com/watch?v=6FjfewWAGdE'));
console.log(GetYoutubeID('https://www.youtube.com/watch?v=6FjfewWAGdE&feature=youtu.be'));
于 2012-10-24T08:10:54.867 回答
-1

你可以随时尝试

youtubeVal.substring(29, 39);
于 2012-10-24T07:11:41.890 回答