Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Extract the YouTube Video ID from a URL in PHP
<?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];
?>
@lateef4web

This comment has been minimized.

Copy link

@lateef4web lateef4web commented Feb 20, 2017

Good bless you and your generation. Thanks alot.

@jon9

This comment has been minimized.

Copy link

@jon9 jon9 commented Mar 31, 2017

I started using this for a text field entry for YouTube, but If someone only enters just the YouTube ID then it doesn't work. How can I make this work if just the ID is entered?
EDIT: I simply added an if strpos condition as a solution.

@myselfgaurav

This comment has been minimized.

Copy link

@myselfgaurav myselfgaurav commented Apr 9, 2017

@jon9 can u explain what u did

@justinliebregts

This comment has been minimized.

Copy link

@justinliebregts justinliebregts commented Apr 10, 2017

@myselfgaurav, my thought is he's just checking if there's something like "http" not found in the string, or "youtu" found, then you can make a guess that it's just a youtube ID being provided and not a full URL.

@muhammadnm

This comment has been minimized.

Copy link

@muhammadnm muhammadnm commented Sep 29, 2017

Awesome.. exactly what I was looking, @ghalusa deserves great thanks!

@aedelgod

This comment has been minimized.

Copy link

@aedelgod aedelgod commented Nov 4, 2017

This was a blessing, thanks for sharing this @ghalusa

@RaihanSojeb

This comment has been minimized.

Copy link

@RaihanSojeb RaihanSojeb commented Nov 26, 2017

@ghalusa what to do for youtube.com/embed/VIDEO_ID

@Galch24

This comment has been minimized.

Copy link

@Galch24 Galch24 commented Feb 13, 2018

Thanks a lot! That's exactly what I need.

@davision

This comment has been minimized.

Copy link

@davision davision commented Mar 2, 2018

May the Lord bless you and returns you in millions.

@MarkTierney1975

This comment has been minimized.

Copy link

@MarkTierney1975 MarkTierney1975 commented Mar 19, 2018

Awesome thanks!

@Urrutia96

This comment has been minimized.

Copy link

@Urrutia96 Urrutia96 commented Apr 9, 2018

Awesome thanks!

@felipefgoncalves

This comment has been minimized.

Copy link

@felipefgoncalves felipefgoncalves commented Apr 26, 2018

How I can use that piece of code in Wordpress loop?

I am trying to use this code to get the ID of a video and insert into my div tag which will display the thumbnail of it.

Here is the current code:

<div class="row">
        <?php 
            $args = array('post_type'=>'videos', 'showposts'=>8); 
            $video = get_posts($args); 
            if($video) : foreach($video as $post) : setup_postdata($post);
        ?>
        <div class="col-xl-3 col-lg-4 col-md-4 col-sm-6">
            <h3 class="titulo_post_pequeno"><?php the_title(); ?></h3>
            <div class="youtube-player" data-id="I NEED THE ID HERE"></div>
        </div>
        <?php
            endforeach;
            endif;
        ?>
</div>
@tejashapani

This comment has been minimized.

Copy link

@tejashapani tejashapani commented Jun 8, 2018

Amazing. Thanks

@mainIine

This comment has been minimized.

Copy link

@mainIine mainIine commented Jun 12, 2018

Tanks a lot. Saved me much time.

@dinhngocvuong

This comment has been minimized.

Copy link

@dinhngocvuong dinhngocvuong commented Jun 16, 2018

Awesome thanks!

@Soullighter

This comment has been minimized.

Copy link

@Soullighter Soullighter commented Jun 20, 2018

One <3, thank you so much!

@apudiu

This comment has been minimized.

Copy link

@apudiu apudiu commented Jul 7, 2018

Wonderful, saved at least an hour or mine.

@kressly

This comment has been minimized.

Copy link

@kressly kressly commented Jul 9, 2018

Thank you very much . This should be posted everywhere

@marivivekraja

This comment has been minimized.

Copy link

@marivivekraja marivivekraja commented Jul 17, 2018

Applause

@OpeOnikute

This comment has been minimized.

Copy link

@OpeOnikute OpeOnikute commented Sep 4, 2018

What happens for a youtube id that contains a hypen "-" ?

@debugsito

This comment has been minimized.

Copy link

@debugsito debugsito commented Oct 23, 2018

How I can use that piece of code in Wordpress loop?

I am trying to use this code to get the ID of a video and insert into my div tag which will display the thumbnail of it.

Here is the current code:

<div class="row">
        <?php 
            $args = array('post_type'=>'videos', 'showposts'=>8); 
            $video = get_posts($args); 
            if($video) : foreach($video as $post) : setup_postdata($post);
        ?>
        <div class="col-xl-3 col-lg-4 col-md-4 col-sm-6">
            <h3 class="titulo_post_pequeno"><?php the_title(); ?></h3>
            <div class="youtube-player" data-id="I NEED THE ID HERE"></div>
        </div>
        <?php
            endforeach;
            endif;
        ?>
</div>

Hi, if $video is the url :

<?php
<div class="row">
        <?php 
            $args = array('post_type'=>'videos', 'showposts'=>8); 
            $video = get_posts($args); 
            if($video) : foreach($video as $post) : setup_postdata($post);
        ?>
        <div class="col-xl-3 col-lg-4 col-md-4 col-sm-6">
            <h3 class="titulo_post_pequeno"><?php the_title(); ?></h3>
           <?php preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $video, $match); ?>
            <div class="youtube-player" data-id="<?php echo $match[1]; ?>"></div>
        </div>
        <?php
            endforeach;
            endif;
        ?>
</div>
@marutitim

This comment has been minimized.

Copy link

@marutitim marutitim commented Oct 30, 2018

Wau this was what I was looking for.so so great.Thanks man

@sakilmahmud

This comment has been minimized.

Copy link

@sakilmahmud sakilmahmud commented Jan 18, 2019

Superb.. Thanks a lot :)

@siawaahmed

This comment has been minimized.

Copy link

@siawaahmed siawaahmed commented Feb 14, 2019

Thank you .

@grajendra

This comment has been minimized.

Copy link

@grajendra grajendra commented Feb 23, 2019

Thank you

@ewsantos

This comment has been minimized.

Copy link

@ewsantos ewsantos commented Feb 27, 2019

Thank you so much!! great help!

@BartMommens

This comment has been minimized.

Copy link

@BartMommens BartMommens commented Mar 13, 2019

Dis Guy! Not all heroes wear capes !!
Thanks m8!

@ndunks

This comment has been minimized.

Copy link

@ndunks ndunks commented Apr 1, 2019

saved my time.

@noelkalel

This comment has been minimized.

Copy link

@noelkalel noelkalel commented Jul 25, 2019

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.

@diarsa

This comment has been minimized.

Copy link

@diarsa diarsa commented Aug 10, 2019

thank you very much! very helpful.

@amjo

This comment has been minimized.

Copy link

@amjo amjo commented Feb 10, 2020

2020 and it's still useful

@ghalusa

This comment has been minimized.

Copy link
Owner Author

@ghalusa ghalusa commented Feb 10, 2020

@amjo Amazing!

@sabeti05

This comment has been minimized.

Copy link

@sabeti05 sabeti05 commented Mar 2, 2020

Thank you!

@dittops

This comment has been minimized.

Copy link

@dittops dittops commented Mar 25, 2020

Thank you, it's useful

@Mohamed-Slimane

This comment has been minimized.

Copy link

@Mohamed-Slimane Mohamed-Slimane commented Apr 28, 2020

Thank you, it's working easy

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented May 5, 2020

2020 and still saving us time. thank you

@ghalusa

This comment has been minimized.

Copy link
Owner Author

@ghalusa ghalusa commented May 5, 2020

@luxaintcc I'll admit, quite incredible! Thank you, and to everyone else!

@jonatanfroes

This comment has been minimized.

Copy link

@jonatanfroes jonatanfroes commented May 14, 2020

I'm here in 2020 to say thanks!

@ghalusa

This comment has been minimized.

Copy link
Owner Author

@ghalusa ghalusa commented May 14, 2020

@jonatanfroes May the regex be with you!

@ijyoung

This comment has been minimized.

Copy link

@ijyoung ijyoung commented May 18, 2020

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

@ijyoung

This comment has been minimized.

Copy link

@ijyoung ijyoung commented May 19, 2020

Resolved - many apologies
Obviously needs
$video=print_r($match[1],TRUE);
echo $video;

@ghalusa

This comment has been minimized.

Copy link
Owner Author

@ghalusa ghalusa commented May 19, 2020

@ijyoung Not a problem! Apologies for not being able to answer right away.

@ijyoung

This comment has been minimized.

Copy link

@ijyoung ijyoung commented May 19, 2020

Thanks for reply.. Much appreciated.

@trevorhawes

This comment has been minimized.

Copy link

@trevorhawes trevorhawes commented Jun 23, 2020

@praveen-vishnu

This comment has been minimized.

Copy link

@praveen-vishnu praveen-vishnu commented Jun 30, 2020

Thanx for posting

@webwayscript

This comment has been minimized.

Copy link

@webwayscript webwayscript commented Oct 27, 2020

embed/live_stream?channel=UCmyKnNRH0wH-r8I-ceP-dsg ???????

@stell

This comment has been minimized.

Copy link

@stell stell commented May 6, 2021

@irfan-dahir

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment