Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save LinuxPanda/5959867 to your computer and use it in GitHub Desktop.
Save LinuxPanda/5959867 to your computer and use it in GitHub Desktop.
Best regex pattern to get youtube video/playlist id with/without time from any youtube link and generate embed code. This regex works with 1) youtube & youtu.be 2) http/https 3) with/without www.
<?php
/*
* @gist Regex to get youtube video/playlist id with/without time & generate embed code
* @license UNLICENSE (http://unlicense.org)
*/
$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;
}
?>
@LinuxPanda
Copy link
Author

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