当前位置:网站首页>PHP seven methods to obtain complete instances of file name suffixes [collect]

PHP seven methods to obtain complete instances of file name suffixes [collect]

2022-06-22 09:02:00 YUJIANYUE

The following schemes are collected from the network and sorted out by myself , Non reprint, non translation, non originality

<?php
$filename = "chalide.com.exe.xlsx";
// Method 1:
function getExt1($filename){
  $type = substr($filename, strrpos($filename, ".")+1);
  return $type;
}
echo "<p>getExt1 " . getExt1($filename) . "</p>";
// Method 2:
function getExt2($filename){
   $type = pathinfo($filename);
   return strtolower($type["extension"]);
}
echo "<p>getExt2 " . getExt2($filename) . "</p>";
// Method 3:
function getExt3($filename){  
   $type =explode("." , $filename);
   $count=count($type)-1;
   return $type[$count];
}
echo "<p>getExt3 " . getExt3($filename) . "</p>";
// Method 4:
function getExt4($filename){
   $arr = explode('.',$filename);
   return array_pop($arr);;
}
echo "<p>getExt4 " . getExt4($filename) . "</p>";
// Method 5:
function getExt5($filename){
   return strrchr($filename,'.');
}
echo "<p>getExt5 " . getExt5($filename) . "</p>";
// Method 6:
function getExt6($filename){
   $str = strrev($filename);
   return strrev(strchr($str,'.',true));
}
echo "<p>getExt6 " . getExt6($filename) . "</p>";
// Method 7:
function getExt7($filename){
   $str = preg_match_all('/\.[a-zA-Z0-9]+$/', $filename, $match);
   return $match[0][0];
}
echo "<p>getExt7 " . getExt7($filename) . "</p>";
// programme 7 You can also use many other regular expressions 、 And explode+ Abandoned end()
?>

原网站

版权声明
本文为[YUJIANYUE]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220523596076.html