Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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))\/(?:watch\?v\=|v\/)?([\w\-]+))/is';
$matches = array();
preg_match($pattern, $source, $matches);
if (isset($matches[1])) return $matches[1];
return false;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment