PHP教程

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

本文主要是介绍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()
?>

这篇关于php七种方法获取文件名后缀完整实例[收集]的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!