获取 youtube 视频 ID 为 PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9973520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 08:00:52  来源:igfitidea点击:

getting youtube video id the PHP

phpurlyoutubegetparse-url

提问by Udders

I am currently writing a webapp in which some pages are heavily reliant on being able to pull the correct youtube video in - and play it. The youtube URLS are supplied by the users and for this reason will generally come in with variants one of them may look like this:

我目前正在编写一个 web 应用程序,其中某些页面严重依赖于能够将正确的 youtube 视频拉入并播放。youtube URLS 由用户提供,因此通常会带有变体,其中之一可能如下所示:

http://www.youtube.com/watch?v=y40ND8kXDlg

http://www.youtube.com/watch?v=y40ND8kXDlg

while the other may look like this:

而另一个可能看起来像这样:

http://www.youtube.com/watch/v/y40ND8kXDlg

http://www.youtube.com/watch/v/y40ND8kXDlg

Currently I am able to pull the ID from the latter using the code below:

目前,我可以使用以下代码从后者中提取 ID:

function get_youtube_video_id($video_id)
{

    // Did we get a URL?
    if ( FALSE !== filter_var( $video_id, FILTER_VALIDATE_URL ) )
    {

        // http://www.youtube.com/v/abcxyz123
        if ( FALSE !== strpos( $video_id, '/v/' ) )
        {
            list( , $video_id ) = explode( '/v/', $video_id );
        }

        // http://www.youtube.com/watch?v=abcxyz123
        else
        {
            $video_query = parse_url( $video_id, PHP_URL_QUERY );
            parse_str( $video_query, $video_params );
            $video_id = $video_params['v'];
        }

    }

    return $video_id;

}

How can I deal with URLS that use the ?vversion rather than the /v/version?

如何处理使用?v版本而不是/v/版本的URL ?

回答by Daniil Ryzhkov

Like this:

像这样:

$link = "http://www.youtube.com/watch?v=oHg5SJYRHA0";
$video_id = explode("?v=", $link);
$video_id = $video_id[1];

Here is universal solution:

这是通用解决方案:

$link = "http://www.youtube.com/watch?v=oHg5SJYRHA0&lololo";
$video_id = explode("?v=", $link); // For videos like http://www.youtube.com/watch?v=...
if (empty($video_id[1]))
    $video_id = explode("/v/", $link); // For videos like http://www.youtube.com/watch/v/..

$video_id = explode("&", $video_id[1]); // Deleting any other params
$video_id = $video_id[0];

Or just use this regex:

或者只是使用这个正则表达式:

(\?v=|/v/)([-a-zA-Z0-9]+)

回答by debasish

<?php
$your_url='https://www.youtube.com/embed/G_5-SqD2gtA';
function get_youtube_id_from_url($url)
{
    if (stristr($url,'youtu.be/'))
        {preg_match('/(https:|http:|)(\/\/www\.|\/\/|)(.*?)\/(.{11})/i', $url, $final_ID); return $final_ID[4]; }
    else 
        {@preg_match('/(https:|http:|):(\/\/www\.|\/\/|)(.*?)\/(embed\/|watch.*?v=|)([a-z_A-Z0-9\-]{11})/i', $url, $IDD); return $IDD[5]; }
}

echo get_youtube_id_from_url($your_url)
?>

回答by Sudhir Bastakoti

Try:

尝试:


function youtubeID($url){
     $res = explode("v",$url);
     if(isset($res[1])) {
        $res1 = explode('&',$res[1]);
        if(isset($res1[1])){
            $res[1] = $res1[0];
        }
        $res1 = explode('#',$res[1]);
        if(isset($res1[1])){
            $res[1] = $res1[0];
        }
     }
     return substr($res[1],1,12);
     return false;
 }
$url = "http://www.youtube.com/watch/v/y40ND8kXDlg";
echo youtubeID($url1);

Should work for both

应该对两者都有效

回答by MichaelRushton

Okay, this is a much better answer than my previous:

好的,这比我以前的答案要好得多:

  $link = 'http://www.youtube.com/watch?v=oHg5SJYRHA0&player=normal';

  strtok($link, '?');

  parse_str(strtok(''));

  echo $v;

It's might be good to have this in a function to keep the new variables out of the global scope (unless you want them there, obviously).

在函数中使用它可能会很好,以将新变量保持在全局范围之外(除非您希望它们在那里,显然)。

回答by Toxic

This may not be in use still, but there might be other people looking for an answer, so, to get a YouTube ID from a URL.

这可能仍然没有使用,但可能还有其他人在寻找答案,因此,从 URL 获取 YouTube ID。

P.S: This works for all types of URL, I've tested it;

PS:这适用于所有类型的 URL,我已经测试过了;

Function getYouTubeID($URL){
    $YouTubeCheck = preg_match('![?&]{1}v=([^&]+)!', $URL . '&', $Data);
    If($YouTubeCheck){
        $VideoID = $Data[1];
    }
    Return $VideoID;
}

Or just use the preg_match function itself;

或者只是使用 preg_match 函数本身;

If(preg_match('![?&]{1}v=([^&]+)!', $URL . '&', $Data)){
        $VideoID = $Data[1];
}

Hope this helps someone :)!

希望这对某人有所帮助:)!

回答by Osama Mosaad

This is best way to get youtube vedio id , Or any field in url , but you must change index (V) from $ID_youtube['v'] to anything you want.

这是获取 youtube 视频 id 或 url 中任何字段的最佳方法,但您必须将索引 (V) 从 $ID_youtube['v'] 更改为您想要的任何内容。

function getID_youtube($url)
{
    parse_str(parse_url($url, PHP_URL_QUERY), $ID_youtube);
    return $ID_youtube['v'];
}

回答by Randel Fordberg

<?php
$link = "http://www.youtube.com/watch?v=oHg5SJYRHA0";
$video_id = str_replace('http://www.youtube.com/watch?v=', '', $link);
echo $video_id;
?>

Output:
oHg5SJYRHA0

输出:
oHg5SJYRHA0

Source

来源

回答by santosh

<?php
$url = "https://www.youtube.com/watch?v=uKW_FPsFiB8&feature=related";
parse_str( parse_url( $url, PHP_URL_QUERY ), $vid );
echo $vid['v'];    

?> 

Output: uKW_FPsFiB8

输出:uKW_FPsFiB8

This will work for urls like https://www.youtube.com/watch?v=uKW_FPsFiB8&feature=relatedor https://www.youtube.com/watch?v=vzH8FH1HF3A&feature=relmfuor only https://www.youtube.com/watch?v=uKW_FPsFiB8

这将适用于https://www.youtube.com/watch?v=uKW_FPsFiB8&feature=relatedhttps://www.youtube.com/watch?v=vzH8FH1HF3A&feature=relmfu或仅https://www.youtube .com/watch?v=uKW_FPsFiB8

回答by Dejan Marjanovic

preg_match("#([\w\d\-]){11}#is", 'http://www.youtube.com/watch?v=y40ND8kXDlg', $matches);
echo $matches[1];

回答by MichaelRushton

$parts = explode('=', $link);

// $parts[1] will y40ND8kXDlg

This example works only if there's one '=' in the URL. Ever likely to be more?

此示例仅在 URL 中有一个“=”时才有效。可能会更多吗?