Home >Backend Development >PHP Tutorial >7 ways to get the suffix name of PHP files

7 ways to get the suffix name of PHP files

零到壹度
零到壹度Original
2018-04-10 17:19:3330496browse

The content of this article is to share with you 7 ways to obtain the suffix name of PHP files. It has certain reference value. Friends in need can refer to it

Recommended manual:php complete self-study manual

First type:

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

Analysis: strrchr($file, '.')
strrchr() function finds the last occurrence of a string in another string and returns the position from that string All characters from position to the end of the string

Second type:

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

Analysis: strrpos($file, '.')
Find the position where "." last appeared in the string and return the position substr() intercepts from this position

The third type:

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

The fourth type:

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

The fifth type:

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

The sixth type:

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

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

Includes the following array elements:
##

[dirname]
[basename]
[extension]

th Seven types:

.$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 article recommendations: 1.
Several methods for PHP to determine and obtain file extensions 2.
Various methods for PHP to obtain file extensions
Related video recommendations:
1. Dugu Jiujian (4)_PHP video tutorial

The above is the detailed content of 7 ways to get the suffix name of PHP files. 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

Related articles

See more