Skip to content

Instantly share code, notes, and snippets.

@krabello
Created August 17, 2018 12:33
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 krabello/4e72c529569b04ce54904c68cab01fb1 to your computer and use it in GitHub Desktop.
Save krabello/4e72c529569b04ce54904c68cab01fb1 to your computer and use it in GitHub Desktop.
PHP Regex to get youtube video ID
<?php
// 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