0

Possible Duplicate:
RegEx pattern to get the YouTube video ID from any YouTube URL

I have stored youtube url in the database.I want to fetch only youtube id from the youtube url.I just want to extract id(6FjfewWAGdE) from below url.

$youtubeVal=http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage
Community
  • 1
  • 1

4 Answers4

1

you can do this by

var regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed)([^#\&\?]*).*/;
var match = url.match(regExp);
if (match&&match[2].length==11){
    return match[2];
} 
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
0

Youtube URLs come in a variety of formats (with the embed/<id> thing or with the watch?v=<id>). Find the URL type(s) you want to understand and build regular expressions to extract them correctly.

koljaTM
  • 10,064
  • 2
  • 40
  • 42
0

Here is a working non-regexp solution in PHP:

function GetYoutubeID($url) {
    $temp = parse_url($url);

    if(isset($temp['query'])) {
        parse_str($temp['query'], $temp2);
        if(isset($temp2['v'])) {
            return $temp2['v'];
        }
    }

    if(isset($temp['path'])) {
        $temp2 = explode("/", $temp['path']);
        foreach($temp2 as $value) {
            if(strlen($value) == 11 || strlen($value) == 10) {
                return $value;
            }
        }
    }

    return "no ID?";
}

echo $youtubeID = GetYoutubeID('http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage') . "\n";
echo $youtubeID = GetYoutubeID('https://www.youtube.com/embed/6FjfewWAGdE') . "\n";
echo $youtubeID = GetYoutubeID('https://youtube.com/embed/6FjfewWAGdE') . "\n";
echo $youtubeID = GetYoutubeID('http://www.youtube.com/watch?v=6FjfewWAGdE') . "\n";
echo $youtubeID = GetYoutubeID('http://www.youtube.com/watch?v=6FjfewWAGdE&feature=player_detailpage') . "\n";
echo $youtubeID = GetYoutubeID('www.youtu.be/6FjfewWAGdE') . "\n";
echo $youtubeID = GetYoutubeID('youtu.be/6FjfewWAGdE') . "\n";
echo $youtubeID = GetYoutubeID('youtube.com/watch?v=6FjfewWAGdE') . "\n";
echo $youtubeID = GetYoutubeID('https://www.youtube.com/watch?v=6FjfewWAGdE&feature=youtu.be') . "\n";

Replace the whole content of the function with the following to use a more better looking solution (Regular expression):

preg_match('/^(http:\/\/|https:\/\/|.*?)(www.|.*?)(youtube.com|youtu.be)\/(embed\/|watch\?v=|.*?)(.*?)(\?|\&|$)/is', $url, $matches);
if(isset($matches[5])) {
    return $matches[5];
}

return "no ID?"

The regular expression solution is also applicable on Javascript:

function GetYoutubeID(url) {
    var regExp = /^(http:\/\/|https:\/\/|.*?)(www.|.*?)(youtube.com|youtu.be)\/(embed\/|watch\?v=|.*?)(.*?)(\?|\&|$)/;
    var match = url.match(regExp);
    if (match){
        return match[5];
    }

    return "no ID?";
}

console.log(GetYoutubeID('http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage'));
console.log(GetYoutubeID('http://www.youtube.com/embed/6FjfewWAGdE?feature=player_detailpage'));
console.log(GetYoutubeID('https://www.youtube.com/embed/6FjfewWAGdE'));
console.log(GetYoutubeID('https://youtube.com/embed/6FjfewWAGdE'));
console.log(GetYoutubeID('http://www.youtube.com/watch?v=6FjfewWAGdE'));
console.log(GetYoutubeID('http://www.youtube.com/watch?v=6FjfewWAGdE&feature=player_detailpage'));
console.log(GetYoutubeID('www.youtu.be/6FjfewWAGdE'));
console.log(GetYoutubeID('youtu.be/6FjfewWAGdE'));
console.log(GetYoutubeID('youtube.com/watch?v=6FjfewWAGdE'));
console.log(GetYoutubeID('https://www.youtube.com/watch?v=6FjfewWAGdE&feature=youtu.be'));
Johan Svensson
  • 863
  • 3
  • 9
  • 23
-1

You can always try with

youtubeVal.substring(29, 39);
Armaggedon
  • 399
  • 4
  • 14