I have the following code which grabs YouTube URLs stored in a string variable:
function getVideoUrlsFromString($html) {
$regex = '#((?:www\.)?(?:youtube\.com\/(?:watch\?v=|embed\/)|youtu\.be\/)([a-zA-Z0-9-]*))#i';
preg_match_all($regex, $html, $matches);
$matches = array_unique($matches[0]);
usort($matches, function($a, $b) {
return strlen($b) - strlen($a);
});
return $matches;
}
$html = 'https://www.youtube-nocookie.com/embed/VWrlXsmcL2E';
$html = getVideoUrlsFromString($html);
print_r($html);
But it doesn't work with:
https://www.youtube-nocookie.com/embed/VWrlXsmcL2E
http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US
Is there any way to alter the regex to work with these 2 common YouTube URLs?