YouTube ID extractor





6
Date Submitted Fri. Jul. 20th, 2007 7:42 PM
Revision 1 of 1
Scripter ColdKeyboard
Tags Extract | ID | youtube
Comments 2 comments
Function that you can use to extract the ID from YouTube links that
look like this:

http://www.youtube.com/watch?v=Fv2aS8YtpmU
http://www.youtube.com/vi/Fv2aS8YtpmU
http://www.youtube.com/?v=Fv2aS8YtpmU

and you can extract this "Fv2aS8YtpmU" so you can use it to get thumbnails that look like : http://img.youtube.com/vi/[videoid]/default.jpg

<?php
function youtubeid($url) {
        if (preg_match('%youtube\\.com/(.+)%', $url, $match)) {
                $match = $match[1];
                $replace = array("watch?v=", "v/", "vi/");
                $match = str_replace($replace, "", $match);
        }
        return $match;
}

//Usage
$video_id = youtubeid("http://www.youtube.com/watch?v=Fv2aS8YtpmU");

echo $video_id;

?>

Sasa Karanovic

Sasa Karanovic
www.MrdniSe.com
www.SasaKaranovic.com

Comments

Comments Nice
Thu. Aug. 23rd, 2007 8:42 AM    Newbie navster
  Comments Youtube Video ID only
Mon. Dec. 24th, 2007 7:16 PM    Newbie mmoraes

Voting