getting youtube video id the PHP

2019-02-18 09:51发布

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:

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

while the other may look like this:

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

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

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 ?v version rather than the /v/ version?

11条回答
仙女界的扛把子
2楼-- · 2019-02-18 10:44
<?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)
?>
查看更多
对你真心纯属浪费
3楼-- · 2019-02-18 10:44

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).

查看更多
淡お忘
4楼-- · 2019-02-18 10:46

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]+)
查看更多
男人必须洒脱
5楼-- · 2019-02-18 10:46

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.

function getID_youtube($url)
{
    parse_str(parse_url($url, PHP_URL_QUERY), $ID_youtube);
    return $ID_youtube['v'];
}
查看更多
一夜七次
6楼-- · 2019-02-18 10:51
<?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

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

查看更多
登录 后发表回答