-
-
Save ghalusa/6c7f3a00fd2383e5ef33 to your computer and use it in GitHub Desktop.
<?php | |
// Here is a sample of the URLs this regex matches: (there can be more content after the given URL that will be ignored) | |
// http://youtu.be/dQw4w9WgXcQ | |
// http://www.youtube.com/embed/dQw4w9WgXcQ | |
// http://www.youtube.com/watch?v=dQw4w9WgXcQ | |
// http://www.youtube.com/?v=dQw4w9WgXcQ | |
// http://www.youtube.com/v/dQw4w9WgXcQ | |
// http://www.youtube.com/e/dQw4w9WgXcQ | |
// http://www.youtube.com/user/username#p/u/11/dQw4w9WgXcQ | |
// http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/0/dQw4w9WgXcQ | |
// http://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ | |
// http://www.youtube.com/?feature=player_embedded&v=dQw4w9WgXcQ | |
// It also works on the youtube-nocookie.com URL with the same above options. | |
// It will also pull the ID from the URL in an embed code (both iframe and object tags) | |
preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match); | |
$youtube_id = $match[1]; | |
?> |
Superb.. Thanks a lot :)
Thank you .
Thank you
Thank you so much!! great help!
Dis Guy! Not all heroes wear capes !!
Thanks m8!
saved my time.
is there any way to extract several yt links?
for instance: lets say i have 3 records saved into db and want them to retrieve and parse on view page, is this doable?
am using laravel btw.
thank you very much! very helpful.
2020 and it's still useful
@amjo Amazing!
Thank you!
Thank you, it's useful
Thank you, it's working easy
2020 and still saving us time. thank you
@luxaintcc I'll admit, quite incredible! Thank you, and to everyone else!
I'm here in 2020 to say thanks!
@jonatanfroes May the regex be with you!
I cannot get the code to work.
Here is draft
$url='https://www.youtube.com/watch?v=1234567';
preg_match('%(?:youtube(?:-nocookie)?.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu.be/)([^"&?/ ]{11})%i', $url, $match);
echo $match[1];
It returns a blank page even if I replace $url with the URL
I would be grateful if you could point me in the right direction
Resolved - many apologies
Obviously needs
$video=print_r($match[1],TRUE);
echo $video;
@ijyoung Not a problem! Apologies for not being able to answer right away.
Thanks for reply.. Much appreciated.
http://www.youtube.com/embed/123456789890 is another one (embed)
Thanx for posting
embed/live_stream?channel=UCmyKnNRH0wH-r8I-ceP-dsg ???????
Not working: https://www.youtube.com/watch?v=-np5iMKQaDw
Not working on https://youtube.com/live/…
Edit:
just added |live/
works for me now:
preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=|live/)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match);
$youtube_id = $match[1];
Not working on https://youtube.com/live/… Edit: just added
|live/
works for me now:preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=|live/)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match); $youtube_id = $match[1];
Works perfect, thanks!
Wau this was what I was looking for.so so great.Thanks man