方法1:使用strrchr()函数
<?php
function getExt($file) {
return strrchr($file, '.');
}
echo getExt('index.php');
?>
注:strrchr() 函数查找字符串在另一个字符串中最后一次出现的位置,并返回从该位置到字符串结尾的所有字符。如果成失败,否则返回 false。
方法2:截取字符串
<?php
function getExt($file) {
return substr($file, strrpos($file, '.'));
}
echo getExt('index.php');
?>
注:strrpos() 函数查找字符串在另一个字符串中最后一次出现的位置。如果成失败,否则返回 false。
方法3:使用数组
<?php
function getExt($file) {
//PHP 5.4开始,会发出警告,因此使用@屏蔽
return @array_pop(explode('.', $file));
}
echo getExt('index.php');
?>
方法4:使用pathinfo()函数
<?php
function getExt($file) {
$temp = pathinfo($file);
return $temp['extension'];
}
echo getExt('index.php');
?>
方法五:
return strrev(substr(strrev($file_name), 0, strpos(strrev($file_name), ‘.’)));
}