Created
November 21, 2013 23:00
-
-
Save simplethemes/7591414 to your computer and use it in GitHub Desktop.
Extracts YouTube video ID from various URL structures
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 | |
$url = array ( | |
'http://youtu.be/dQw4w9WgXcA', | |
'http://www.youtube.com/embed/dQw4w9WgXcB', | |
'http://www.youtube.com/watch?v=dQw4w9WgXcC', | |
'http://www.youtube.com/?v=dQw4w9WgXcD', | |
'http://www.youtube.com/v/dQw4w9WgXcE', | |
'http://www.youtube.com/e/dQw4w9WgXcF', | |
'http://www.youtube.com/user/username#p/u/11/dQw4w9WgXcG', | |
'http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/0/dQw4w9WgXcH', | |
'http://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcI', | |
'http://www.youtube.com/?feature=player_embedded&v=dQw4w9WgXcJ' | |
); | |
// Extracts the YouTube ID from various URL structures | |
foreach ($url as $value) { | |
if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $value, $match)) { | |
$id = $match[1]; | |
var_dump($id); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment