Skip to content

Instantly share code, notes, and snippets.

@FriendlyWP
Created June 24, 2015 23:03
Show Gist options
  • Save FriendlyWP/3b83b478945436fce6c7 to your computer and use it in GitHub Desktop.
Save FriendlyWP/3b83b478945436fce6c7 to your computer and use it in GitHub Desktop.
Get YouTube ID from various url structures
// GET YOUTUBE ID FROM URL
function getYouTubeIdFromURL($url) {
$pattern = '%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i';
preg_match($pattern, $url, $matches);
return isset($matches[1]) ? $matches[1] : false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment