function getVideoCode($url){
$videoCode;
$code_parse = parse_url($url);
if(empty($code_parse["query"])){
$videoCode = str_replace("/"," ",$code_parse["path"]);
}else{
$videoCode = clearQuery($code_parse["query"]);
}
echo $videoCode;
}
function clearQuery($query){
$redundant = array("v", "&", "feature","=","related");
return str_replace($redundant," ",$query);
}
这不是专业代码,但很容易理解。当我这样调用时:
getVideoCode("http://youtu.be/8uLPtmCroQ8");
getVideoCode("http://www.youtube.com/watch?v=8uLPtmCroQ8");
getVideoCode("http://www.youtube.com/watch?v=8uLPtmCroQ8&feature=related");
输出是
- 8uLPtmCroQ8
- 8uLPtmCroQ8
- 8uLPtmCroQ8