This article mainly introduces the method of PHP get file extension, combined with the example form summarizes 6 commonly used file extension methods, code with more detailed comments for easy to understand, the need for friends can refer to the next
This article summarizes how PHP gets the file name extension. Share to everyone for your reference, as follows:
In the PHP interview or the exam will have a great chance to write five ways to get the file extension, here are some of my own summary of the methods
$file = ' file to get the extension. php ';//The first, according to. Split, gets the value of the last element function Getext1{return end (Explode (".", $file);)}//Second, gets the position of the last point , Intercept function Getext2{return substr ($file, Strrpos ($file, '. ') +1);} The third, according to the. Split, gets the value of the last element function GetExt3 ($file) {return Array_pop (Explode ('. ', $file));} The fourth type, PathInfo function GetExt5 ($file) {$arr = PathInfo ($file); return $arr [' extension '];//or so return PathInfo ($file, pathinfo_extension);} Fifth, regular, sub-mode function Getext6$file) {Preg_match ("/(gif | jpg | png) $/", $file, $match); $match = $match [0];}//Sixth, The regular reverse reference function GetExt7 ($file) {$match =preg_replace ("/.*\. ( \w+)/"," \\1 ", $file); echo $match;}
Related recommendations:
Common ways to get file extensions in PHP
Multiple ways to get file extensions in PHP
How to use PHP for file name extension judgment