Created
September 12, 2018 20:25
-
-
Save wilnaweb/10d39212d08189edf0dd934afe266d31 to your computer and use it in GitHub Desktop.
Get Youtube Video ID By URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function _get_youtube_id($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]; | |
} | |
} | |
} | |
return $video_id; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment