Home > Backend Development > PHP Tutorial > too easy! 7 ways to get file extension in PHP

too easy! 7 ways to get file extension in PHP

silencement
Release: 2023-04-08 10:00:01
forward
2717 people have browsed it

too easy! 7 ways to get file extension in PHP

How to get the file extension in PHP

First method:

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

Analysis: strrchr($file, '.' )

strrchr() function finds the last occurrence of a string in another string and returns all characters from that position to the end of the string

Second :

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

Analysis: strrpos($file, '.')

Find the last occurrence of "." in the string and return the position substr() Intercept from this position

The third type:

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

The fourth type:

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

The fifth type:

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

Sixth type:

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

Analysis: The pathinfo() function returns the file path information in the form of an array.

Includes the following array elements:

[dirname]
[basename]
[extension]
Copy after login

Seventh type:

.$file = 'x.y.z.png';
echo pathinfo($file, PATHINFO_EXTENSION)
Copy after login

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

Related labels:
php
source:www.liqingbo.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