if (isset($url_parts["query"]) && (strpos($url_parts["query"], "v") !== false))
{
parse_str($url_parts["query"], $vars);
// Handle full URLs with query string like 'http://www.youtube.com/watch?v=KMU0tzLwhbE'
if (isset($vars["v"]) && $vars["v"])
{
$video_code = $vars["v"]; // Handle the new short URLs like 'http://youtu.be/KMU0tzLwhbE'
} else if ($url_parts['path'])
{
$video_code = trim($url_parts['path'], '/');
}
}
Quick reference for Grabbing video IDs from Youtube and Vimeo.
*Note $string is the string containing youtube or vimeo link, can also contain rubbish.
** Note2: Video ID will be return in $output. Different PregMatch will result in Video ID being located differently in $output.
Youtube
There¡¯s 2 formats, the latter youtu.be was launch fairly recently. Both formats uses the same video ID so it¡¯s just 2 different extraction methods.