How to embed YouTube videos in PHP?
You have to ask users to store the 11 character code from the youtube video.
For e.g. http://www.youtube.com/watch?v=Ahg6qcgoay4
The eleven character code is : Ahg6qcgoay4
You then take this code and place it in your database. Then wherever you want to place the youtube video in your page, load the character from the database and put the following code:-
e.g. for Ahg6qcgoay4 it will be :
<object width="425" height="350" data="http://www.youtube.com/v/Ahg6qcgoay4" type="application/x-shockwave-flash"><param name="src" value="http://www.youtube.com/v/Ahg6qcgoay4" /></object>
The <object>
and <embed>
tags are deprecated as per HTML Youtube Videos, it is preferable to use the <iframe>
tag to do so.
<iframe width="420" height="315"
src="http://www.youtube.com/embed/XGSy3_Czz8k?autoplay=1">
</iframe>
In order for your users not spend their entire lifetime trying to find the video id in links to put it in your form field, let them post the link of the video they find on youtube and you can use the following regex to obtain the video id:
preg_match("/^(?:http(?:s)?:\/\/)?
(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|
(?:embed|v|vi|user)\/))([^\?&\"'>]+)/", $url, $matches);
To get the video id you can fetch it $matches[1]
, these will match:
- youtube.com/v/vidid
- youtube.com/vi/vidid
- youtube.com/?v=vidid
- youtube.com/?vi=vidid
- youtube.com/watch?v=vidid
- youtube.com/watch?vi=vidid
- youtu.be/vidid
- youtube.com/embed/vidid
- http://youtube.com/v/vidid
- http://www.youtube.com/v/vidid
- https://www.youtube.com/v/vidid
- youtube.com/watch?v=vidid&wtv=wtv
- http://www.youtube.com/watch?dev=inprogress&v=vidid&feature=related
- https://m.youtube.com/watch?v=vidid
Part of this answer is referred by @shawn's answer in this question.
From both long and short youtube urls you can get the embed this way:
$ytarray=explode("/", $videolink);
$ytendstring=end($ytarray);
$ytendarray=explode("?v=", $ytendstring);
$ytendstring=end($ytendarray);
$ytendarray=explode("&", $ytendstring);
$ytcode=$ytendarray[0];
echo "<iframe width=\"420\" height=\"315\" src=\"http://www.youtube.com/embed/$ytcode\" frameborder=\"0\" allowfullscreen></iframe>";
Hope it helps someone
Do not store the embed code in your database -- YouTube may change the embed code and URL parameters from time to time. For example the <object>
embed code has been retired in favor of <iframe>
embed code. You should parse out the video id from the URL/embed code (using regular expressions, URL parsing functions or HTML parser) and store it. Then display it using whatever mechanism currently offered by YouTube API.
A naive PHP example for extracting the video id is as follows:
<?php
preg_match(
'/[\\?\\&]v=([^\\?\\&]+)/',
'http://www.youtube.com/watch?v=OzHvVoUGTOM&feature=channel',
$matches
);
// $matches[1] should contain the youtube id
?>
I suggest that you look at these articles to figure out what to do with these ids:
- Embed YouTube Videos, Playlists and More with IFrame Embeds
To create your own YouTube video player:
- YouTube Embedded Player Parameters
- YouTube JavaScript Player API Reference