php七种方法获取文件名后缀完整实例[收集]

以下方案均收集自网络并经本人整理,非转载非翻译非原创

<?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()
?>

    原文作者:YUJIANYUE
    原文地址: https://blog.csdn.net/YUJIANYUE/article/details/120602798
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞