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/