Skip to content

Instantly share code, notes, and snippets.

@morales2k
Last active August 29, 2015 14:04
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 morales2k/d60997e7b88bbbf8e0c8 to your computer and use it in GitHub Desktop.
Save morales2k/d60997e7b88bbbf8e0c8 to your computer and use it in GitHub Desktop.
Youtube regex to extract video id from most youtube urls... from stack overflow answer: http://stackoverflow.com/a/17030234/1591301
$yturl = 'http://youtu.be/0f4qZkd4npM';
/*
other possible urls...
youtube.com/v/vidid
youtube.com/vi/vidid
youtube.com/?v=vidid
youtube.com/?vi=vidid
youtube.com/watch?v=vidid
youtube.com/watch?vi=vidid
youtu.be/vidid
youtube.com/embed/vidid
http://youtube.com/v/vidid
http://www.youtube.com/v/vidid
https://www.youtube.com/v/vidid
youtube.com/watch?v=vidid&wtv=wtv
http://www.youtube.com/watch?dev=inprogress&v=vidid&feature=related
*/
preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/", $yturl, $possible_id);
echo "<pre>"; print_r($possible_id); echo "</pre>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment