Cách lấy Youtube video ID từ URL

Cập nhật lần cuối 11/03/2017 by trong PHP vào 27/04/2015 có 1141 Views

Dưới đây là function giúp chúng ta lấy được youtube video id từ URL cho trước.

getyoutubevideoID

 

Demo online

Function lấy Youtube Video ID

function getYouTubeVideoId($url)
{
$video_id = false;
$url = parse_url($url);
if (strcasecmp($url['host'], 'youtu.be') === 0)
{
#### (dontcare)://youtu.be/<video id>
$video_id = substr($url['path'], 1);
}
elseif (strcasecmp($url['host'], 'www.youtube.com') === 0)
{
if (isset($url['query']))
{
parse_str($url['query'], $url['query']);
if (isset($url['query']['v']))
{
#### (dontcare)://www.youtube.com/(dontcare)?v=<video id>
$video_id = $url['query']['v'];
}
}
if ($video_id == false)
{
$url['path'] = explode('/', substr($url['path'], 1));
if (in_array($url['path'][0], array('e', 'embed', 'v')))
{
#### (dontcare)://www.youtube.com/(whitelist)/<video id>
$video_id = $url['path'][1];
}
}
}else{
return false;
}
return $video_id;
}

Chạy thử

Code sau để test hàm lấy ID của video youtube phía trên.

$urls = array(
'http://youtu.be/dQw4w9WgXcQ',
'http://www.youtube.com/?v=dQw4w9WgXcQ',
'http://www.youtube.com/?v=dQw4w9WgXcQ&feature=player_embedded',
'http://www.youtube.com/watch?v=dQw4w9WgXcQ',
'http://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=player_embedded',
'http://www.youtube.com/v/dQw4w9WgXcQ',
'http://www.youtube.com/e/dQw4w9WgXcQ',
'http://www.youtube.com/embed/dQw4w9WgXcQ',
'http://www.youtube.com/watch?t=40s&v=dQw4w9WgXcQ',
'https://levantoan.com',
'https://www.youtube.com/watch?v=u8nQa1cJyX8&list=RDu8nQa1cJyX8#t=45'
);
foreach ($urls as $url)
{
echo sprintf('%s -> %s <br>' . PHP_EOL, $url, getYouTubeVideoId($url));
}

File hoàn thiện

<?php
function getYouTubeVideoId($url)
{
$video_id = false;
$url = parse_url($url);
if (strcasecmp($url['host'], 'youtu.be') === 0)
{
#### (dontcare)://youtu.be/<video id>
$video_id = substr($url['path'], 1);
}
elseif (strcasecmp($url['host'], 'www.youtube.com') === 0)
{
if (isset($url['query']))
{
parse_str($url['query'], $url['query']);
if (isset($url['query']['v']))
{
#### (dontcare)://www.youtube.com/(dontcare)?v=<video id>
$video_id = $url['query']['v'];
}
}
if ($video_id == false)
{
$url['path'] = explode('/', substr($url['path'], 1));
if (in_array($url['path'][0], array('e', 'embed', 'v')))
{
#### (dontcare)://www.youtube.com/(whitelist)/<video id>
$video_id = $url['path'][1];
}
}
}else{
return false;
}
return $video_id;
}
$urls = array(
'http://youtu.be/dQw4w9WgXcQ',
'http://www.youtube.com/?v=dQw4w9WgXcQ',
'http://www.youtube.com/?v=dQw4w9WgXcQ&feature=player_embedded',
'http://www.youtube.com/watch?v=dQw4w9WgXcQ',
'http://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=player_embedded',
'http://www.youtube.com/v/dQw4w9WgXcQ',
'http://www.youtube.com/e/dQw4w9WgXcQ',
'http://www.youtube.com/embed/dQw4w9WgXcQ',
'http://www.youtube.com/watch?t=40s&v=dQw4w9WgXcQ',
'https://levantoan.com',
'https://www.youtube.com/watch?v=u8nQa1cJyX8&list=RDu8nQa1cJyX8#t=45'
);
foreach ($urls as $url)
{
echo sprintf('%s -> %s <br>' . PHP_EOL, $url, getYouTubeVideoId($url));
}
?>

Kết quả

Sau khi có hàm get youtube video id và code để test chúng ta sẽ có kết quả như bên dưới.

http://youtu.be/dQw4w9WgXcQ -> dQw4w9WgXcQ
http://www.youtube.com/?v=dQw4w9WgXcQ -> dQw4w9WgXcQ
http://www.youtube.com/?v=dQw4w9WgXcQ&feature=player_embedded -> dQw4w9WgXcQ
http://www.youtube.com/watch?v=dQw4w9WgXcQ -> dQw4w9WgXcQ
http://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=player_embedded -> dQw4w9WgXcQ
http://www.youtube.com/v/dQw4w9WgXcQ -> dQw4w9WgXcQ
http://www.youtube.com/e/dQw4w9WgXcQ -> dQw4w9WgXcQ
http://www.youtube.com/embed/dQw4w9WgXcQ -> dQw4w9WgXcQ
http://www.youtube.com/watch?t=40s&v=dQw4w9WgXcQ -> dQw4w9WgXcQ
https://levantoan.com ->
https://www.youtube.com/watch?v=u8nQa1cJyX8&list=RDu8nQa1cJyX8#t=45 -> u8nQa1cJyX8

Chúc các bạn thành công!

Cách lấy Youtube video ID từ URL
5 (100%) 4 votes
  • Bình luận
Hosting nên dùng cho WordPress tại Việt Nam
Có thể bạn quan tâm
x