本篇文章為大家展示了使用PHP怎么對文件的擴(kuò)展名進(jìn)行獲取,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
在PHP面試中或者考試中會(huì)有很大幾率碰到寫出五種獲取文件擴(kuò)展名的方法,下面是我自己總結(jié)的一些方法
$file = '需要進(jìn)行獲取擴(kuò)展名的文件.php'; //第一種,根據(jù).拆分,獲取最后一個(gè)元素的值 function getExt1{ return end(explode(".",$file);) } //第二種,獲取最后一個(gè)點(diǎn)的位置,截取 function getExt2{ return substr($file,strrpos($file,'.')+1); } //第三種,根據(jù).拆分,獲取最后一個(gè)元素的值 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; }