Get the video url from YouTube and strip off all the parameters
Hello how can I delete anything of this string and have only the XBH1dcHoL6Y ?
<param name="movie" value="http://www.youtube.com/v/XBH1dcHoL6Y&rel=0&hl=en_US&feature=player_embedded&version=3">
My purpose is to have the url like this
http://www.youtube.com/watch?v=XBH1dcHo开发者_运维百科L6Y
and this is what I found so far (but i can't delete the parameters)
$url = "http://www.youtube.com/v/6n8PGnc_cV4";
$start = strpos($url,"v=");
echo 'http://www.youtube.com/v/'.substr($url,$start+2);
Thank you!
Obligatory one-liner:
echo end(explode('/', reset(explode('&', 'http://www.youtube.com/v/XBH1dcHoL6Y&rel=0&hl=en_US&feature=player_embedded&version=3'))));
Edit: preg_match version:
$string = '<embed src="http://www.youtube.com/v/XBH1dcHoL6Y&rel=0&hl=en_US&feature=player_embedded&-version=3" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="640" height="390"></embed>';
$expr = "/<embed.*https?:\/\/www.youtube.com\/v\/([a-zA-Z0-9]+).*<\/embed>/";
if(preg_match($expr, $string, $matches))
echo 'Matched: '.$matches[1];
else
echo 'No match';
// returns "Matched: XBH1dcHoL6Y"
try this:
$url = "http://www.youtube.com/v/XBH1dcHoL6Y&rel=0&hl=en_US";
$urlSplode = explode('/',$url);
$urlEnd = $urlSplode[count($urlSplode)-1];
$urlEndSplode = explode('&',$urlEnd);
$finalStr = $urlEndSplode[0];
echo "http://www.youtube.com/watch?v=$finalStr";
here is the demo:
http://codepad.org/aoGLZSX0
$urlTokens = explode("/", "http://www.youtube.com/v/XBH1dcHoL6Y&rel=0&hl=en_US&feature=player_embedded&version=3");
$urlTokens = explode("&", $urlTokens[4]);
$url = $urlTokens[0]; // will output XBH1dcHoL6Y
精彩评论