Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
PHP function to get youtube ID from URL
function get_youtube_video_ID($youtube_video_url) {
* Pattern matches
$pattern =
(?:youtube # Match any youtube url www or no www , https or no https
(?:-nocookie)?\.com/ # allows for the nocookie version too.
(?:[^/]+/.+/ # Once we have that, find the slashes
|(?:v|e(?:mbed)?)/|.*[?&]v=) # Check if its a video or if embed
|youtu\.be/) # Allow short URLs
([^"&?/ ]{11}) # Once its found check that its 11 chars.
// Checks if it matches a pattern and returns the value
if (preg_match($pattern, $youtube_video_url, $match)) {
return $match[1];
// if no match return false.
return false;

This comment has been minimized.

Copy link

commented Mar 25, 2019 doesn't return ID

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.