page contents

七种PHP获取文件扩展名的方法

PHP中获取文件扩展名的方法

PHP中获取文件扩展名的方法


第一种:

$file = 'x.y.z.png';echo substr(strrchr($file'.'), 1);

析:strrchr($file, '.')

strrchr() 函数查找字符串在另一个字符串中最后一次出现的位置,并返回从该位置到字符串结尾的所有字符


第二种:

$file = 'x.y.z.png';echo substr($file, strrpos($file, '.')+1);

析:strrpos($file, '.')

查找 "." 在字符串中最后一次出现的位置,返回位置 substr()从该位置开始截取


第三种:

$file = 'x.y.z.png';$arr = explode('.', $file);echo $arr[count($arr)-1];


第四种:

$file = 'x.y.z.png';$arr = explode('.', $file);echo end($arr);  //end()返回数组的最后一个元素


五种:

$file = 'x.y.z.png';echo strrev(explode('.', strrev($file))[0]);


第六种:

.$file = 'x.y.z.png';echo pathinfo($file)['extension'];

解析:pathinfo() 函数以数组的形式返回文件路径的信息。

包括以下的数组元素:

[dirname][basename][extension]


第七种:

.$file = 'x.y.z.png';echo pathinfo($file, PATHINFO_EXTENSION)
  • 发表于 2020-03-12 11:20
  • 阅读 ( 411 )
  • 分类:PHP开发

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
Pack
Pack

1135 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 1316 文章
  3. Pack 1135 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章