Skip to content

Instantly share code, notes, and snippets.

@ghalusa
Created June 20, 2015 23:14
Show Gist options
  • Save ghalusa/6c7f3a00fd2383e5ef33 to your computer and use it in GitHub Desktop.
Save ghalusa/6c7f3a00fd2383e5ef33 to your computer and use it in GitHub Desktop.
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];
?>
@OpeOnikute
Copy link

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

@debugsito
Copy link

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
Copy link

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

@sakilmahmud
Copy link

Superb.. Thanks a lot :)

@siawaahmed
Copy link

Thank you .

@grajendra
Copy link

Thank you

@ewsantos
Copy link

Thank you so much!! great help!

@BartMommens
Copy link

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

@ndunks
Copy link

ndunks commented Apr 1, 2019

saved my time.

@noelkalel
Copy link

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
Copy link

diarsa commented Aug 10, 2019

thank you very much! very helpful.

@amjo
Copy link

amjo commented Feb 10, 2020

2020 and it's still useful

@ghalusa
Copy link
Author

ghalusa commented Feb 10, 2020

@amjo Amazing!

@sabeti05
Copy link

sabeti05 commented Mar 2, 2020

Thank you!

@dittops
Copy link

dittops commented Mar 25, 2020

Thank you, it's useful

@Mohamed-Slimane
Copy link

Thank you, it's working easy

Copy link

ghost commented May 5, 2020

2020 and still saving us time. thank you

@ghalusa
Copy link
Author

ghalusa commented May 5, 2020

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

@jonatanfroes
Copy link

I'm here in 2020 to say thanks!

@ghalusa
Copy link
Author

ghalusa commented May 14, 2020

@jonatanfroes May the regex be with you!

@ijyoung
Copy link

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
Copy link

ijyoung commented May 19, 2020

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

@ghalusa
Copy link
Author

ghalusa commented May 19, 2020

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

@ijyoung
Copy link

ijyoung commented May 19, 2020

Thanks for reply.. Much appreciated.

@trevorhawes
Copy link

@praveen-vishnu
Copy link

Thanx for posting

@webwayscript
Copy link

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

@stell
Copy link

stell commented May 6, 2021

@irfan-dahir
Copy link

@harihar-w
Copy link

harihar-w commented Sep 29, 2023

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];

@neurotic
Copy link

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!

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