$file = "test.php";
- 方式1 explode()函数
-
//字符串转换成数组
-
$arr = explode('.',$file);
-
echo $arr[count($arr)-1];
- 方式2 strrpos()函数
-
//最后一次出现位置
-
echo substr($file, strrpos($file, '.')+1);
- 方式3 strrchr()函数
-
//最后一次出现的位置
-
echo substr(strrchr($file,'.'),1);
- 方式4 explode()函数 strrev()函数
-
//将字符串倒叙切割为数组 再把数组下标为0的倒叙输出
-
echo strrev(explode('.', strrev($file))[0]);
- 方式5 end()函数
-
//end()返回数组的最后一个元素
-
$arr=explode('.', $file);
-
echo end($arr);
- 方式6 pathinfo()函数
-
/*
-
* 函数以数组的形式返回文件路径的信息
-
*
-
* 包括以下的数组元素:
-
* [dirname]
-
* [basename]
-
* [extension]
-
*
-
* 可选。规定要返回的数组元素。默认是 all。
-
* 可能的值:
-
* PATHINFO_DIRNAME - 只返回 dirname
-
* PATHINFO_BASENAME - 只返回 basename
-
* PATHINFO_EXTENSION - 只返回 extension
-
*/
-
//两种方式都可以
-
echo pathinfo($file, PATHINFO_EXTENSION);
-
echo pathinfo($file)['extension'];
- 方式7 array_pop()函数
-
$arr=explode('.', $file);
-
echo array_pop($arr);