Skip to content

Instantly share code, notes, and snippets.

@jaredatch
Created June 10, 2015 20:34
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jaredatch/7f0aa8eefe19a3a937b1 to your computer and use it in GitHub Desktop.
Save jaredatch/7f0aa8eefe19a3a937b1 to your computer and use it in GitHub Desktop.
Extract YouTube ID from URL
<?php
/**
* Fetch YouTube ID from URLs
*
* @param string $url
* @return string
*/
function ja_youtube_id( $url ){
$regex = '~(?:http|https|)(?::\/\/|)(?:www.|)(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/ytscreeningroom\?v=|\/feeds\/api\/videos\/|\/user\S*[^\w\-\s]|\S*[^\w\-\s]))([\w\-]{11})[a-z0-9;:@#?&%=+\/\$_.-]*~i';
$id = preg_replace( $regex, '$1', $url );
return $id;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment