YouTube: generate embed from video URL with PHP

$url = 'http://www.youtube.com/watch?v=fHBFvlQ3JGY';
preg_match(
        '/[\\?\\&]v=([^\\?\\&]+)/',
        $url,
        $matches
    );
$id = $matches[1];
 
$width = '640';
$height = '385';
echo '<object width="' . $width . '" height="' . $height . '"><param name="movie" value="http://www.youtube.com/v/' . $id . '&amp;hl=en_US&amp;fs=1?rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' . $id . '&amp;hl=en_US&amp;fs=1?rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' . $width . '" height="' . $height . '"></embed></object>';

Reference

Feedback

Thanks for that, mate!
pvjl
Jun 2, 2012
#1
This is what i was searching... :) Thanks alot... its working well and good... :)
uma
Mar 1, 2013
#2
To use YouTube's new (2012) embed code, which is HTML5 and mobile compatible, replace the last line with this:

echo '&lt;iframe width="' . $width . '" height="' . $height . '" src="http://www.youtube.com/embed/' . $id . '?autoplay=1" frameborder="0" allowfullscreen&gt;&lt;/iframe>'

LouieGeetoo
May 8, 2013
#3
Replace &lt; with a less-than sign in my code above to use it.
LouieGeetoo
May 8, 2013
#4
Thanks A Lot.....
You Saved Me..

www.havock.co.in
www.havock.org
Havock Web Designers
Aug 17, 2013
#5
Thanks a ton man
Vineet
Jan 27, 2014
#6
thanks
it`s working
kaushik baravaliya
Feb 7, 2014
#7
Thank you, and @LouieGeetoo, good work!!
Holgz
May 9, 2014
#8
I would prefer iframes over the objects.
PS: A complete solution to this issue would be https://github.com/dereuromark/MediaEmbed which can extract the "host slug" and "id", stores those in the DB and completes the output HTML upon display (either as iframe - preferred - or as object fallback).
mark
Jul 21, 2014
#9
Thanks alot.Thank you so much for this .....
Athira
Aug 6, 2014
#10
It's great.Working nicely.. Thank you very much..
Chamith
Aug 28, 2015
#11
Thank you so much for this code..
swadesh
Sep 28, 2015
#12
thanks

Dec 11, 2015
#13
how about handling https://youtu.be/fHBFvlQ3JGY and https://www.youtube.com/embed/fHBFvlQ3JGY
using that method, it returns undefined offset:1

pls show how to handle this
florence
Jan 12, 2016
#14
I think i finally found a solution that played nice for the three types of urls u can get from youtube. Here is my code:

function getYoutubeEmbedUrl($url)
{
$shortUrlRegex = '/youtu.be\/([a-zA-Z0-9_]+)\??/i';
$longUrlRegex = '/youtube.com\/((?:embed)|(?:watch))((?:\?v\=)|(?:\/))(\w+)/i';

if (preg_match($longUrlRegex, $url, $matches)) {
$id = $matches[count($matches) - 1];
}

if (preg_match($shortUrlRegex, $url, $matches)) {
$id = $matches[count($matches) - 1];
}

return isset($id) ? $id : 'error';
}

here, $shortUrlRegex takes care of https://youtu.be/CY6SdyMr3Ac
and $longUrlRegex, https://www.youtube.com/watch?v=CY6SdyMr3Ac and https://www.youtube.com/embed/CY6SdyMr3Ac

Hope this helps somebody!
Florence Okosun
Jan 13, 2016
#15
Very Good Thanks...
Pankaj Singh
May 4, 2016
#16
undefined offset 1 error...

why this happens?
danij
Aug 23, 2016
#17