Rootop 服务器运维与web架构

php获取文件名扩展两种方法

// 文件
$file = '/admin/login/index.php';

// 方法1
// 1 获取文件名
$a = basename($file);
// 2 获取扩展名
$b = strrchr($a, '.');
// 3 去掉扩展名中的点
$c = substr($b, 1);
echo $c . "<br/>";

//合并起来
echo substr(strrchr(basename($file), '.'), 1) . "<br/>";


// 方法2 利用 pathinfo
var_dump(pathinfo($file));

array(4) {
  'dirname' =>
  string(12) "/admin/login"
  'basename' =>
  string(9) "index.php"
  'extension' =>
  string(3) "php"
  'filename' =>
  string(5) "index"
}

可以看到是一个数据,直接调key
echo pathinfo($file)['extension'];

原创文章,转载请注明。本文链接地址: https://www.rootop.org/pages/3551.html

作者:Venus

服务器运维与性能优化

评论已关闭。