Php 如何从youtube url获取youtube id?

Php 如何从youtube url获取youtube id?,php,javascript,Php,Javascript,可能重复: 我已将youtube url存储在数据库中。我只想从youtube url获取youtube id。我只想从下面的url提取id(6FjfewWAGdE) $youtubeVal=http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage 你可以这样做 var regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed)([^#\&\?]*).*/; var match =

可能重复:

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

$youtubeVal=http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage
你可以这样做

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

你可以试着用它

youtubeVal.substring(29, 39);

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

这是一个在PHP中运行的非regexp解决方案:

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";
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'));
将函数的全部内容替换为以下内容,以使用外观更好的解决方案():

正则表达式解决方案也适用于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";
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'));

查看regular Expression我认为教人们在PHP中使用“var”不是一个好主意,因为它从5.3开始就被弃用,并在5.4中被删除。@JohanSvensson oops抱歉更正了抱歉,我不确定。。。他将其标记为Javascript和PHP。你发布的是JavaScript,我没有检查太多。但正则表达式在所有示例中都不起作用:/