Skip to content

Instantly share code, notes, and snippets.

@krabello
Created December 22, 2015 14:21
Show Gist options
  • Save krabello/d3f055dd6e8292cb0983 to your computer and use it in GitHub Desktop.
Save krabello/d3f055dd6e8292cb0983 to your computer and use it in GitHub Desktop.
PHP Regex to get youtube video ID
/*
Matches:
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
https://m.youtube.com/watch?v=vidid
*/
preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/", $url, $matches);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment