p-mcmw
Last Updated: February 25, 2016
·
1.344K
· aalaap
Aalaap filter 512

Easiest way to extract the video ID from a YouTube URL

I went through a lot of snippets that perform various kinds of RegEx and parse_url and what not, but I think, since most YouTube video IDs are 11 characters, why not just look for the only 11-character string and extract that?

function quickYouTubeId($youtubeurl) {
    preg_match("#[a-zA-Z0-9-]{11}#", $youtubeurl, $id);
    return (strlen($id[0])==11) ? $id[0] : false;
}

I can't think of a YouTube URL that'll foil this match. Can you?

This is also on GitHub: https://gist.github.com/3187024

Say Thanks
Respond