首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从youtube url中获取youtube id?

如何从youtube url中获取youtube id?
EN

Stack Overflow用户
提问于 2012-10-24 07:06:31
回答 4查看 481关注 0票数 0

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

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

代码语言:javascript
复制
$youtubeVal=http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage
EN

回答 4

Stack Overflow用户

发布于 2012-10-24 07:11:15

你可以通过

代码语言:javascript
复制
var regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed)([^#\&\?]*).*/;
var match = url.match(regExp);
if (match&&match[2].length==11){
    return match[2];
} 
票数 1
EN

Stack Overflow用户

发布于 2012-10-24 07:14:42

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

票数 0
EN

Stack Overflow用户

发布于 2012-10-24 08:10:54

下面是PHP中的非regexp解决方案。

代码语言:javascript
复制
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";

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

代码语言:javascript
复制
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

代码语言: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'));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13044364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档