Skip to content

Instantly share code, notes, and snippets.

@typeoneerror
Created May 6, 2014 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save typeoneerror/6d90d31529c5e8e871d2 to your computer and use it in GitHub Desktop.
Save typeoneerror/6d90d31529c5e8e871d2 to your computer and use it in GitHub Desktop.
Simple function for matching any youtube link and grabbing the ID
if (!function_exists('video_id_from_youtube_url')):
function video_id_from_youtube_url($source)
{
$pattern = '/^(?:(?:(?:https?:)?\/\/)?(?:www.)?(?:youtu(?:be.com|.be))\/(?:watch\?v\=|v\/)?([\w\-]+))/is';
$matches = array();
preg_match($pattern, $source, $matches);
if (isset($matches[1])) return $matches[1];
return false;
}
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment