7 ways to get file extensions in PHP

不言
Release: 2023-03-23 22:02:01
Original
4481 people have browsed it

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template