以下方案均收集自网络并经本人整理,非转载非翻译非原创
<?php
$filename = "chalide.com.exe.xlsx";
//方法1:
function getExt1($filename){
$type = substr($filename, strrpos($filename, ".")+1);
return $type;
}
echo "<p>getExt1 " . getExt1($filename) . "</p>";
//方法2:
function getExt2($filename){
$type = pathinfo($filename);
return strtolower($type["extension"]);
}
echo "<p>getExt2 " . getExt2($filename) . "</p>";
//方法3:
function getExt3($filename){
$type =explode("." , $filename);
$count=count($type)-1;
return $type[$count];
}
echo "<p>getExt3 " . getExt3($filename) . "</p>";
//方法4:
function getExt4($filename){
$arr = explode('.',$filename);
return array_pop($arr);;
}
echo "<p>getExt4 " . getExt4($filename) . "</p>";
//方法5:
function getExt5($filename){
return strrchr($filename,'.');
}
echo "<p>getExt5 " . getExt5($filename) . "</p>";
//方法6:
function getExt6($filename){
$str = strrev($filename);
return strrev(strchr($str,'.',true));
}
echo "<p>getExt6 " . getExt6($filename) . "</p>";
//方法7:
function getExt7($filename){
$str = preg_match_all('/\.[a-zA-Z0-9]+$/', $filename, $match);
return $match[0][0];
}
echo "<p>getExt7 " . getExt7($filename) . "</p>";
//方案7还可以使用很多其他正则表达式、及explode+已弃用的end()
?>