【问题标题】:How to fetch youtube id from the youtube url? [duplicate]如何从 youtube url 获取 youtube id? [复制]
【发布时间】:2012-10-24 07:06:31
【问题描述】:

可能重复:
RegEx pattern to get the YouTube video ID from any YouTube URL

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

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

【问题讨论】:

  • 查看正则表达式

标签: php javascript


【解决方案1】:

你可以这样做

var regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed)([^#\&\?]*).*/;
var match = url.match(regExp);
if (match&&match[2].length==11){
    return match[2];
} 

【讨论】:

  • 我认为教人们在 PHP 中使用“var”并不是一个好主意,因为它自 5.3 以来已被弃用并在 5.4 中被删除。
  • @JohanSvensson 哎呀抱歉弄错了
  • 对不起,我不确定...他已经将它标记为 Javascript 和 PHP。您发布的是 JavaScript,我没有检查足够的内容。但是正则表达式在所有示例中仍然不起作用:/
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    这是 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";
    

    将函数的全部内容替换为以下内容,以使用更好看的解决方案 (Regular expression):

    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'));
    

    【讨论】:

      【解决方案4】:

      你可以随时尝试

      youtubeVal.substring(29, 39);
      

      【讨论】:

        猜你喜欢
        • 2019-02-20
        • 2012-05-22
        • 2011-03-28
        • 2012-06-23
        • 1970-01-01
        • 2023-03-27
        • 2019-12-07
        • 2016-01-21
        相关资源
        最近更新 更多