如何使用PHP从嵌入URL获取视频ID? [重复]

This question already has an answer here:

I am having an youtube embed URL like this

<object width="420" height="315">
<param name="movie" value="//www.youtube.com/v/whTwjG4ZIJg?version=3&hl=en_US"></param>
<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>
<embed src="//www.youtube.com/v/whTwjG4ZIJg?version=3&hl=en_US" type="application/x-shockwave-flash" width="420" height="315" allowscriptaccess="always" allowfullscreen="true">
</embed></object>

Now i want to get ID of video Using PHP. Please someone help

</div>

You could use something like this to get the ID of the youtube link:

$your_url='http://www.youtube.com/watch?var1=blabla#v=GvJehZx3eQ1$var2=bla';

function get_youtube_id_from_url($url){
    if (stristr($url,'youtu.be/'))
        { preg_match('/(https:|http:|)(\/\/www\.|\/\/|)(.*?)\/(.{11})/i', $url, $final_ID); return $final_ID[4]; }
    else 
        { preg_match('/(https:|http:|):(\/\/www\.|\/\/|)(.*?)\/(embed\/|watch\?v=|(.*?)&v=|v\/|e\/|.+\/|watch.*v=|)([a-z_A-Z0-9]{11})/i', $url, $IDD); return $IDD[6]; }
}

echo get_youtube_id_from_url($your_url);

Output:

GvJehZx3eQ1

And something like this to get the youtube link from the embed tags:

$sLink = "<embed src=\"www.youtube.com/v/whTwjG4ZIJg?version=3&hl=en_US\"></embed>";
preg_match('/< *embed[^>]*src *= *["\']?([^"\']*)/i', $sLink,$aMatch);
echo $aMatch[1];

Output:

www.youtube.com/v/whTwjG4ZIJg?version=3&hl=en_US

You will have to use a little bit of your logic to make both these functions work together :) But this is a good starting point!

Bear in mind that the " will need to be escaped with a \ if you are enclosing the expression in double quotes.

P.S: Got the first function from somewhere, just don't remember where.

EDIT

You could also use Javascript for this:

var re = /(\?v=|\/\d\/|\/embed\/|\/v\/|\.be\/)([a-zA-Z0-9\-\_]+)/;
var urlArr = [
    "http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index",
    "http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o",              
];
for (var i = 0; i < urlArr.length; i++) {
    alert(urlArr[i].match(re)[2]);
}

JSFiddle: http://jsfiddle.net/A7LYZ/

Since you're dealing with tags you could also use DOMDocument to get that value. You can do it like this:

$html = '<object width="420" height="315"><param name="movie" value="//www.youtube.com/v/whTwjG4ZIJg?version=3&hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="//www.youtube.com/v/whTwjG4ZIJg?version=3&hl=en_US" type="application/x-shockwave-flash" width="420" height="315" allowscriptaccess="always" allowfullscreen="true"></embed></object>';
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_clear_errors();
$youtube_link = $dom->getElementsByTagName('embed')->item(0)->getAttribute('src');
echo $youtube_link; // //www.youtube.com/v/whTwjG4ZIJg?version=3&hl=en_US