Philipp
Philipp

Reputation: 65

preg_match to find out YouTube ID

I already got it working to find out the YouTube ID of an normal YouTube URL. But I didn't thought about the short youtu.be URLs and I don't get it woking to filter out the YouTube-Id the video. As far as I know there are these kinds of YT-URLs available:

youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index

youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o

youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0

youtube.com/watch?v=0zM3nApSvMg#t=0m10s

youtube.com/embed/0zM3nApSvMg?rel=0

youtube.com/watch?v=0zM3nApSvMg

youtu.be/0zM3nApSvMg

I found this in another question but it's not working:

/^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/

So my question is: How do I have to edit the following code the get always the YouTube-ID back?! ;)

Thank you so much!

add_filter('embed_oembed_html', 'my_embed_oembed_html', 99, 4);
function my_embed_oembed_html( $html, $url, $attr, $post_id) {
    $posttext = substr($url[0],1);  
    preg_match('/v\=([a-zA-Z0-9,-]+)/', $url, $youtubeID);   

  return '<div id="video" class="border-frame"><video class="video" data-settings="autoresize:fit" preload="none" data-youtube-id="' . $youtubeID[1] . '"></video></div>';
}

Upvotes: 1

Views: 1396

Answers (2)

Matt Friedman
Matt Friedman

Reputation: 1563

Actually, you might consider not using regular expressions. PHP has lots of built in functions that can help you. For instance, try the following code:

$urls = array(
    'youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index',
    'youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o',
    'youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0',
    'youtube.com/watch?v=0zM3nApSvMg#t=0m10s',
    'youtube.com/embed/0zM3nApSvMg?rel=0',
    'youtube.com/watch?v=0zM3nApSvMg',
    'youtu.be/0zM3nApSvMg'
);

function displayId($url, $id) {
    echo "URL: ", $url, "\n";
    echo "ID:  ", $id, "\n\n";
}

foreach ($urls as $url) {

    $u = parse_url($url);

    parse_str($u['query'], $queryVars);

    if ($u['query'] && $queryVars['v']) {
        displayId($url, $queryVars['v']);
    } else if ($u['fragment']) {
        displayId($url, basename($u['fragment']));
    } else if ($u['path']) {
        displayId($url, basename($u['path']));
    }
}

This produces the following output:

URL: youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index
ID:  0zM3nApSvMg

URL: youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o
ID:  QdK8U-VIH_o

URL: youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0
ID:  0zM3nApSvMg

URL: youtube.com/watch?v=0zM3nApSvMg#t=0m10s
ID:  0zM3nApSvMg

URL: youtube.com/embed/0zM3nApSvMg?rel=0
ID:  0zM3nApSvMg

URL: youtube.com/watch?v=0zM3nApSvMg
ID:  0zM3nApSvMg

URL: youtu.be/0zM3nApSvMg
ID:  0zM3nApSvMg

The code could be more robust but I think it gives you the idea. :)

Cheers.

Upvotes: 2

PrivateUser
PrivateUser

Reputation: 4524

add_filter('embed_oembed_html', 'my_embed_oembed_html', 99, 4);
function my_embed_oembed_html( $html, $url, $attr, $post_id) {
    $posttext = substr($url[0],1); 
    preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $url, $youtubeID);   

  return '<div id="video" class="border-frame"><video class="video" data-settings="autoresize:fit" preload="none" data-youtube-id="' . $youtubeID[1] . '"></video></div>';
}

Upvotes: 1

Related Questions