Skip to content

Instantly share code, notes, and snippets.

@wilnaweb
Created September 12, 2018 20:25
Show Gist options
  • Save wilnaweb/10d39212d08189edf0dd934afe266d31 to your computer and use it in GitHub Desktop.
Save wilnaweb/10d39212d08189edf0dd934afe266d31 to your computer and use it in GitHub Desktop.
Get Youtube Video ID By URL
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