PHP Get File Extension Example
First:
$file = 'x.y.z.png'; echo substr(strrchr($file, '.'), 1);
Parsing: 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 type:
$file = 'x.y.z.png'; echo substr($file, strrpos($file, '.')+1);
Analysis: strrpos($file, '.')
Find the last occurrence of "." in the string, and return the position substr() from that Position start interception
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]);
Sixth type:
.$file = 'x.y.z.png'; echo pathinfo($file)['extension'];
Analysis: The pathinfo() function returns the file path information in the form of an array.
Includes the following array elements:
[dirname] [basename] [extension]
The seventh type:
.$file = 'x.y.z.png'; echo pathinfo($file, PATHINFO_EXTENSION);
Summary: I personally prefer the seventh one Kind of
Recommended tutorial: "PHP Video Tutorial"
The above is the detailed content of PHP get file extension instance parsing. For more information, please follow other related articles on the PHP Chinese website!