Skip to content

Instantly share code, notes, and snippets.

@bferronato
Forked from simplethemes/youtube-url.php
Created May 30, 2016 12:33
Show Gist options
  • Save bferronato/11987d940960f565ce5364dbabe31826 to your computer and use it in GitHub Desktop.
Save bferronato/11987d940960f565ce5364dbabe31826 to your computer and use it in GitHub Desktop.
Extracts YouTube video ID from various URL structures
<?php
$url = array (
'http://youtu.be/dQw4w9WgXcA',
'http://www.youtube.com/embed/dQw4w9WgXcB',
'http://www.youtube.com/watch?v=dQw4w9WgXcC',
'http://www.youtube.com/?v=dQw4w9WgXcD',
'http://www.youtube.com/v/dQw4w9WgXcE',
'http://www.youtube.com/e/dQw4w9WgXcF',
'http://www.youtube.com/user/username#p/u/11/dQw4w9WgXcG',
'http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/0/dQw4w9WgXcH',
'http://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcI',
'http://www.youtube.com/?feature=player_embedded&v=dQw4w9WgXcJ'
);
// Extracts the YouTube ID from various URL structures
foreach ($url as $value) {
if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $value, $match)) {
$id = $match[1];
var_dump($id);
}
}
?>
@bferronato
Copy link
Author

bferronato commented May 31, 2016

$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;
}

fonte: https://linuxpanda.wordpress.com/2013/07/24/ultimate-best-regex-pattern-to-get-grab-parse-youtube-video-id-from-any-youtube-link-url/

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