getting youtube video id the PHP
30,720
Solution 1
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]+)
Solution 2
<?php
// Here is a sample of the URLs this regex matches: (there can be more content after the given URL that will be ignored)
// http://youtu.be/dQw4w9WgXcQ
// http://www.youtube.com/embed/dQw4w9WgXcQ
// http://www.youtube.com/watch?v=dQw4w9WgXcQ
// http://www.youtube.com/?v=dQw4w9WgXcQ
// http://www.youtube.com/v/dQw4w9WgXcQ
// http://www.youtube.com/e/dQw4w9WgXcQ
// http://www.youtube.com/user/username#p/u/11/dQw4w9WgXcQ
// http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/0/dQw4w9WgXcQ
// http://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ
// http://www.youtube.com/?feature=player_embedded&v=dQw4w9WgXcQ
// It also works on the youtube-nocookie.com URL with the same above options.
// It will also pull the ID from the URL in an embed code (both iframe and object tags)
preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match);
$youtube_id = $match[1];
?>
Solution 3
<?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)
?>
Solution 4
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
Solution 5
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).
Author by
Udders
Updated on August 25, 2022Comments
-
Udders over 1 year
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:
while the other may look like this:
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? -
Aleks G about 12 yearsIf you copy/paste urls from youtube website, there are often other parameters, such as `youtube.com/watch?v=oHg5SJYRHA0&player=normal&... There can be a dozen of these.
-
Aleks G about 12 yearsThis will not always work. If you copy/paste urls from youtube website, there are often other parameters, such as
http://www.youtube.com/watch?v=oHg5SJYRHA0&player=normal&... There can be a dozen of these. Your code will produce the ID of
oHg5SJYRHA0&player=normal&...` - which is clearly wrong -
Aleks G about 12 yearsThis might work with the
/
URL, but will not always work with the other type. If you copy/paste urls from youtube website, there are often other parameters, such as youtube.com/watch?v=oHg5SJYRHA0&player=normal&... There can be a dozen of these. Judge for yourself what will happen if you search for the last=
-
Daniil Ryzhkov about 12 yearsFixed that. Look at the new solution.
-
Maik about 12 yearsoh thats right. but if you search for the first appearance of "v=" or "v/" you get the start of the id. the end of the id is the end of the string or the next "&".
-
ling over 8 yearsOr look at my code here: github.com/lingtalfi/video-ids-and-thumbnails/blob/master/… It handles various youtube urls, and vimeo and dailymotion too.