以不少于三種方式,獲取文件擴(kuò)展名
// $file = __FILE__;
$file = "x.y.z.php";
function getExt1($file) {
return pathinfo($file)['extension'];
}
echo getExt1($file), "<hr>";
function getExt2($file) {
return pathinfo($file, PATHINFO_EXTENSION);
}
echo getExt2($file), "<hr>";
function getExt3($file) {
// $file = "x.y.z.php";
// strstr 的對(duì)應(yīng)函數(shù) strrchr
// return strstr($file, "."); // .y.z.php
// strrchr
// r = reverse 相反的,顛倒的
// return strrchr($file, "."); // .php
// 方式一:
// return substr(strrchr($file, "."), "1");
// 方式二:
// trim($str); 默認(rèn)清除空格,第二個(gè)擴(kuò)展參數(shù),指定清除對(duì)象
// ltrim l = left 在左邊清除
// rtrim r = right 在右邊清除
// return trim(strrchr($file, "."), ".");
return ltrim(strrchr($file, "."), ".");
}
echo getExt3($file), "<hr>";
function getExt4($file) {
// $file = "x.y.z.php";
// strpos 從左往右查找指定字符的第一個(gè)位置(下標(biāo))
// return strpos($file, ".");
// strrpos() 從右往左查找指定字符的第一個(gè)位置(下標(biāo))
// return strrpos($file, ".");
return substr($file, strrpos($file, ".")+1);
}
echo getExt4($file), "<hr>";
function getExt5($file) {
// $file = "x.y.z.php";
$arr = explode(".", $file);
// count()的同名函數(shù)sizeof() 語(yǔ)法糖
// return $arr[count($arr)-1];
return $arr[sizeof($arr)-1];
}
echo getExt5($file), "<hr>";
function getExt6($file) {
$arr = explode(".", $file);
return end($arr);
}
echo getExt6($file), "<hr>";