Home  >  Article  >  Backend Development  >  7 ways to get file extensions in PHP

7 ways to get file extensions in PHP

不言
不言Original
2018-04-16 10:10:434423browse

This article introduces the 7 methods of obtaining file suffix names in PHP. It has certain reference value. Now I share it with you. Friends in need can refer to it

1.$file = 'x.y.z.png';
echo substr(strrchr($file, '.'), 1);
解析:strrchr($file, '.')   
 strrchr() 函数查找字符串在另一个字符串中最后一次出现的位置,并返回从该位置到字符串结尾的所有字符
2.$file = 'x.y.z.png';
echo substr($file, strrpos($file, '.')+1);
解析:strrpos($file, '.')   
查找 "." 在字符串中最后一次出现的位置,返回位置   substr()从该位置开始截取
3.$file = 'x.y.z.png';
$arr=explode('.', $file);
echo $arr[count($arr)-1];
4.$file = 'x.y.z.png';
$arr=explode('.', $file);
echo end($arr);  //end()返回数组的最后一个元素
5.$file = 'x.y.z.png';
echo strrev(explode('.', strrev($file))[0]);
6.$file = 'x.y.z.png';
echo pathinfo($file)['extension'];
解析:pathinfo() 函数以数组的形式返回文件路径的信息。包括以下的数组元素:
[dirname]
[basename]
[extension]
7.$file = 'x.y.z.png';
echo pathinfo($file, PATHINFO_EXTENSION);

Summary: 2 types of string interception, 3 types of array splitting, 2 types of path functions

Related recommendations:

php Get some time practice with the implementation


The above is the detailed content of 7 ways to get file extensions in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn