To embed youtube videos in a website, the best solution is to parse a string to match for youtube url and then grab the youtube VIDEO_ID or PLAYLIST_ID from the url and then use that video id in a standard embed code.
This is one of the ultimately best regex (regular expression) pattern to get/grab/parse youtube video id from any youtube link/url.
This regex works
- with or without http/https
- with or without www
- with both youtube.com & youtu.be links
This regex can be used to grab
- youtube video id
- youtube playlist id
Regex to grab Video ID:
<?php
//Video id is 11 characters in length
$video_pattern = '~(?:http|https|)(?::\/\/|)(?:www.|)(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/ytscreeningroom\?v=|\/feeds\/api\/videos\/|\/user\S*[^\w\-\s]|\S*[^\w\-\s]))([\w\-]{11})[a-z0-9;:@#?&%=+\/\$_.-]*~i';
$youtube_url = array(
"https://youtu.be/yVpbFMhOAwE",
"https://www.youtube.com/embed/yVpbFMhOAwE",
"youtu.be/yVpbFMhOAwE",
"youtube.com/watch?v=yVpbFMhOAwE",
"http://youtu.be/yVpbFMhOAwE",
"http://www.youtube.com/embed/yVpbFMhOAwE",
"http://www.youtube.com/watch?v=yVpbFMhOAwE",
"http://www.youtube.com/watch?v=yVpbFMhOAwE&feature=g-vrec",
"http://www.youtube.com/watch?v=yVpbFMhOAwE&feature=player_embedded",
"http://www.youtube.com/v/yVpbFMhOAwE?fs=1&hl=en_US",
"http://www.youtube.com/ytscreeningroom?v=yVpbFMhOAwE",
"http://www.youtube.com/watch?NR=1&feature=endscreen&v=yVpbFMhOAwE",
"http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo",
"http://www.youtube.com/watch?v=6zUVS4kJtrA&feature=c4-overview-vl&list=PLbzoR-pLrL6qucl8-lOnzvhFc2UM1tcZA",
"https://www.youtube.com/watch?v=FZu097wb8wU&list=RDFZu097wb8wU"
);
foreach($youtube_url as $youtube_url) {
$youtube_id = (preg_replace($video_pattern, '$1', $youtube_url));
var_dump ($youtube_id);
}
?>
Regex to grab Playlist ID:
<?php
//Playlist id is 12 or more characters in length
$playlist_pattern = '~(?:http|https|)(?::\/\/|)(?:www.|)(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/ytscreeningroom\?v=|\/feeds\/api\/videos\/|\/user\S*[^\w\-\s]|\S*[^\w\-\s]))([\w\-]{12,})[a-z0-9;:@#?&%=+\/\$_.-]*~i';
$youtube_url = array(
"http://www.youtube.com/watch?v=6zUVS4kJtrA&feature=c4-overview-vl&list=PLbzoR-pLrL6qucl8-lOnzvhFc2UM1tcZA",
"https://www.youtube.com/watch?v=FZu097wb8wU&list=RDFZu097wb8wU"
);
foreach($youtube_url as $youtube_url) {
$playlist_id = (preg_replace($playlist_pattern, '$1', $youtube_url));
var_dump ($playlist_id);
}
?>
If you look closely then you’ll find that both the regex are same except for the word count. We just change the word count to parse either video or playlist id.
We use “11” to get the video id and “12,” to get the playlist id (12 followed by comma means 12 char or more).
The explanation of the actual regex:
~ |
(?:http|https|) |
Optional scheme. Either http or https |
(?::\/\/|) |
Optional scheme. :// |
(?:www.|) |
Optional scheme. www. |
(?: |
Group host alternatives |
youtu\.be\/ |
Either youtu.be |
|youtube\.com |
youtube.com |
(?: |
Group path alternatives. |
\/embed\/ |
/embed |
|\/v\/ |
or /v |
|\/watch\?v= |
/watch?v= |
|\/ytscreeningroom\?v= |
/ytscreeningroom?v= |
|\/feeds\/api\/videos\/ |
or either /feeds or /api or /videos |
|\/user |
or /user |
\S* |
Allow anything upto “youtube VIDEO_ID” (Note: 1a) |
[^\w\-\s] |
but char before “youtube VIDEO_ID” is non-id char (Note: 2a) |
|\S* |
or Allow anything upto “youtube VIDEO_ID” (Note: 1b) |
[^\w\-\s] |
but char before “youtube VIDEO_ID” is non-id char (Note: 2b) |
) |
End group path alternatives |
) |
End host alternatives |
([\w\-]{11}) |
“youtube VIDEO_ID” is exactly 11 chars |
([\w\-]{12,}) |
“youtube PLAYLIST_ID” is exactly 12 or more chars |
[a-z0-9;:@?&%=+\/\$_.-]* |
Strip out anything left in the url |
~i |
Note:
- 1a & 2a – These are required to get video id from this url format -> “youtube.com/watch?NR=1&feature=endscreen&v=yVpbFMhOAwE”
- 1b & 2b – These are required to get video id from this url format -> “youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo”
Convert time to seconds for embedding:
The following code will parse youtube video links with time identifier, extract the time and convert it into seconds.
<php?
// Check for time identifier in the youtube video link and conver it into seconds
function ConvertTimeToSeconds($data)
{
$time = null;
$hours = null;
$minutes = null;
$seconds = null;
$pattern_time_split = "([0-9]{1-2}+[^hms])";
// Regex to check for youtube video link with time identifier
$youtube_time = '~(?:http|https|)(?::\/\/|)(?:www.|)(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/ytscreeningroom\?v=|\/feeds\/api\/videos\/|\/user\S*[^\w\-\s]|\S*[^\w\-\s]))([\w\-]{11})[a-z0-9;:@#?&%=+\/\$_.-]*(t=((\d+h)?(\d+m)?(\d+s)?))~i';
// Check for time identifier in the youtube video link, extract it and convert it to seconds
if (preg_match($youtube_time, $data, $matches)) {
// Check for hours
if (isset($matches[4])) {
$hours = $matches[4];
$hours = preg_split($pattern_time_split, $hours);
$hours = substr($hours[0], 0, -1);
}
// Check for minutes
if (isset($matches[5])) {
$minutes = $matches[5];
$minutes = preg_split($pattern_time_split, $minutes);
$minutes = substr($minutes[0], 0, -1);
}
// Check for seconds
if (isset($matches[6])) {
$seconds = $matches[6];
$seconds = preg_split($pattern_time_split, $seconds);
$seconds = substr($seconds[0], 0, -1);
}
// Convert time to seconds
$time = (($hours*3600) + ($minutes*60) + $seconds);
}
return $time;
}
?>
The complete code to parse for video/playlist id and generate the embed code:
<?php
$youtube_url = array(
"https://youtu.be/yVpbFMhOAwE",
"https://www.youtube.com/embed/yVpbFMhOAwE",
"youtu.be/yVpbFMhOAwE",
"youtube.com/watch?v=yVpbFMhOAwE",
"http://youtu.be/yVpbFMhOAwE&t=2m",
"http://www.youtube.com/embed/yVpbFMhOAwE&t=2m5s",
"http://www.youtube.com/watch?v=yVpbFMhOAwE",
"http://www.youtube.com/watch?v=yVpbFMhOAwE&feature=g-vrec&t=30s",
"http://www.youtube.com/watch?v=yVpbFMhOAwE&feature=player_embedded",
"http://www.youtube.com/v/yVpbFMhOAwE?fs=1&hl=en_US",
"http://www.youtube.com/ytscreeningroom?v=yVpbFMhOAwE",
"http://www.youtube.com/watch?NR=1&feature=endscreen&v=yVpbFMhOAwE",
"http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo",
"http://www.youtube.com/watch?v=6zUVS4kJtrA&feature=c4-overview-vl&list=PLbzoR-pLrL6qucl8-lOnzvhFc2UM1tcZA",
"https://www.youtube.com/watch?v=FZu097wb8wU&list=RDFZu097wb8wU"
);
foreach($youtube_url as $youtube_url) {
$data = AutoParseYoutubeLink($youtube_url);
var_dump ($data);
}
// Automatically parse youtube video/playlist links and generate the respective embed code
function AutoParseYoutubeLink($data)
{
// Check if youtube link is a playlist
if (strpos($data, 'list=') !== false) {
// Generate the embed code
$data = preg_replace('~(?:http|https|)(?::\/\/|)(?:www.|)(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/ytscreeningroom\?v=|\/feeds\/api\/videos\/|\/user\S*[^\w\-\s]|\S*[^\w\-\s]))([\w\-]{12,})[a-z0-9;:@#?&%=+\/\$_.-]*~i', 'https://www.youtube.com/embed/videoseries?list=$1', $data);
return $data;
}
// Check if youtube link is not a playlist but a video [with time identifier]
if (strpos($data, 'list=') === false && strpos($data, 't=') !== false) {
$time_in_secs = null;
// Get the time in seconds from the time function
$time_in_secs = ConvertTimeToSeconds($data);
// Generate the embed code
$data = preg_replace('~(?:http|https|)(?::\/\/|)(?:www.|)(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/ytscreeningroom\?v=|\/feeds\/api\/videos\/|\/user\S*[^\w\-\s]|\S*[^\w\-\s]))([\w\-]{11})[a-z0-9;:@#?&%=+\/\$_.-]*~i', 'https://www.youtube.com/embed/$1?start=' . $time_in_secs, $data);
return $data;
}
// If the above conditions were false then the youtube link is probably just a plain video link. So generate the embed code already.
$data = preg_replace('~(?:http|https|)(?::\/\/|)(?:www.|)(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/ytscreeningroom\?v=|\/feeds\/api\/videos\/|\/user\S*[^\w\-\s]|\S*[^\w\-\s]))([\w\-]{11})[a-z0-9;:@#?&%=+\/\$_.-]*~i', 'https://www.youtube.com/embed/$1', $data);
return $data;
}
// Check for time identifier in the youtube video link and conver it into seconds
function ConvertTimeToSeconds($data)
{
$time = null;
$hours = null;
$minutes = null;
$seconds = null;
$pattern_time_split = "([0-9]{1-2}+[^hms])";
// Regex to check for youtube video link with time identifier
$youtube_time = '~(?:http|https|)(?::\/\/|)(?:www.|)(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/ytscreeningroom\?v=|\/feeds\/api\/videos\/|\/user\S*[^\w\-\s]|\S*[^\w\-\s]))([\w\-]{11})[a-z0-9;:@#?&%=+\/\$_.-]*(t=((\d+h)?(\d+m)?(\d+s)?))~i';
// Check for time identifier in the youtube video link, extract it and convert it to seconds
if (preg_match($youtube_time, $data, $matches)) {
// Check for hours
if (isset($matches[4])) {
$hours = $matches[4];
$hours = preg_split($pattern_time_split, $hours);
$hours = substr($hours[0], 0, -1);
}
// Check for minutes
if (isset($matches[5])) {
$minutes = $matches[5];
$minutes = preg_split($pattern_time_split, $minutes);
$minutes = substr($minutes[0], 0, -1);
}
// Check for seconds
if (isset($matches[6])) {
$seconds = $matches[6];
$seconds = preg_split($pattern_time_split, $seconds);
$seconds = substr($seconds[0], 0, -1);
}
// Convert time to seconds
$time = (($hours*3600) + ($minutes*60) + $seconds);
}
return $time;
}
?>
Thanks a Lot…….
Happy to hear that this article helped you, thanks. 🙂
Works great! Thank you for sharing!
Thanks and I’m happy to hear that this article was useful for you. 🙂
THANKS YOU!!!
previous i have used
%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^”&?/ ]{11})%i
but with your regex now can get id from link:
http://www.youtube.com/watch?feature=player_detailpage&v=yyyyyyyyyyy
useful thanks!!
Happy to hear that this article was useful for you. 🙂
Sorry but this regex check id from this also… There is a way to adapt regex to avoid check this links?
youtube.com/channel/xxxxxxxxxxxxxxxxxxxx/featured/
Now i have fixed with
if (!stristr($matches['youtube'][0][$key],'channel/')
Thanks for sharing this info. 🙂
many many many thanks dude its perfect!!!!!!!
Happy to hear that this article was useful for you. :]
The only one regexp solution I found working for all URLs! many thanks!
Happy to hear that this article was useful for you. 🙂
Thanks, this helped! I used it in preg_replace to zap out youtube links in worpdress excerpts (I’m using Toolsets Types/Views but can be easily modified):
add_filter('wpv_filter_post_excerpt', 'remove_youtubelinks_func');
function remove_youtubelinks_func($excerpt){
return $excerpt = preg_replace('~(?:http|https|)(?::\/\/|)(?:www.|)(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/ytscreeningroom\?v=|\/feeds\/api\/videos\/|\/user\S*[^\w\-\s]|\S*[^\w\-\s]))([\w\-]{11})[a-z0-9;:@?&%=+\/\$_.-]*~i', '', $excerpt);
}
Happy to hear that this article was useful for you. And thank you very much for sharing the information about using it with wordpress excerpts. 🙂
It desn’t work with urls like this http://www.youtube.com/watch?v=yVpbFMhOAwE&feature=youtu.be
Just now I checked it and the regex does work with links like http://www.youtube.com/watch?v=yVpbFMhOAwE&feature=youtu.be.
Thank you very much for the information, chief… Works as sweet as a charm… Bookmarked and everything…
Thanks for the feedback.
This article quite amazing. But i have a problem . I need the same codes in c#. Please help me! That may be the last chance of me 😦