在PHP面試中或者考試中會有很大概率碰到寫出五種獲取文件擴展名的方法php
$file = ‘須要進行獲取擴展名的文件.php’;面試
//第一種,根據.拆分,獲取最後一個元素的值
function getExt1{
return end(explode(".",$file);)
}
//第二種,獲取最後一個點的位置,截取
function getExt2{
return substr($file,strrpos($file,'.')+1);
}
//第三種,根據.拆分,獲取最後一個元素的值
function getExt3($file) {
return array_pop(explode(‘.’,$file));
}
//第四種,pathinfo
function getExt5($file) {
$arr = pathinfo($file);
return $arr['extension'];
//或者這樣return pathinfo($file,PATHINFO_EXTENSION);
}get
//第五種,正則,子模式
function getExt6$file){
preg_match("/(gif | jpg | png)$/",$file,$match);
$match=$match[0];
}
//第六種,正則反向引用
function getExt7($file){
$match=preg_replace("/.*\.(\w+)/" , "\\1" ,$file );
echo $match;
}io