Skip to content

Instantly share code, notes, and snippets.

@cdowebcast
Forked from saxap/parse youtube url
Created May 19, 2016 17:58
Show Gist options
  • Save cdowebcast/044e7be981560bf62ce33f80a844a565 to your computer and use it in GitHub Desktop.
Save cdowebcast/044e7be981560bf62ce33f80a844a565 to your computer and use it in GitHub Desktop.
parse youtube url and return video id
function parse_youtube_url($url) {
$pattern = '#^(?:https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch\?v=|/watch\?.+&v=))([\w-]{11})(?:.+)?$#x';
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