Created
August 17, 2018 12:33
-
-
Save krabello/4e72c529569b04ce54904c68cab01fb1 to your computer and use it in GitHub Desktop.
PHP Regex to get youtube video ID
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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