本文實例總結了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);}//第五種,正則,子模式function getExt6$file){preg_match("/(gif | jpg | png)$/",$file,$match);$match=$match[0];} //第六種,正則反向引用function getExt7($file){$match=preg_replace("/.*/.(/w+)/" , "//1" ,$file );echo $match;}希望本文所述對大家PHP程序設計有所幫助。
新聞熱點
疑難解答
圖片精選