问题描述
|
这个问题已经在这里有了答案:
解决方法
看到答案:
$ext = pathinfo($filename,PATHINFO_EXTENSION);
, 您可以使用路径信息查询。
$info = pathinfo($file);
哪里
$info[\'extension\']
包含扩展名
, 您可以定义如下函数:
function get_file_extension($filename)
{
/*
* \".\" for extension should be available and not be the first character
* so position should not be false or 0.
*/
$lastDotPos = strrpos($fileName,\'.\');
if ( !$lastDotPos ) return false;
return substr($fileName,$lastDotPos+1);
}
或者您可以使用PHP内置的Spl_FileInfo对象
, 您要使用正则表达式:
preg_match(\'/\\.([^\\.]+)$/\',$filename);
您可以在此处进行测试,看看是否可以根据输入获得所需的结果。
, 有很多方法可以做到这一点,即使用explode()或preg_match和其他方法。
但是我的方法是使用pathinfo:
$path_info = pathinfo($filename);
echo $path_info[\'extension\'],\"\\n\";
, 您可以使用。爆炸字符串,然后获取最后一个数组项:
$filename = \"file.m.e.ext\";
$filenameitems = explode(\".\",$filename);
echo $filenameitems[count($filenameitems) - 1]; // .ext
// or echo $filenameitem[-1];