This article mainly introduces how to obtain the file extension in PHP, and summarizes 6 common file extension retrieval methods in the form of examples. the code is provided with more detailed annotations for ease of understanding, for more information about how to obtain the file extension in PHP, see the next article. six common methods for obtaining the file extension are summarized in the form of examples, the code is provided with more detailed annotations for ease of understanding. For more information, see
This example summarizes how PHP obtains the file extension. We will share this with you for your reference. The details are as follows:
During the PHP interview or examination, there will be a great chance of writing five methods to get the file extension. Below are some of my summary methods.
$ File = 'File to obtain the extension. php '; // The first type, according. split, get the value of the last element function getExt1 {return end (explode (". ", $ file);)} // second, obtain the position of the last vertex, and intercept function getExt2 {return substr ($ file, strrpos ($ file ,'. ') + 1);} // The third type, according. split, get the value of the last element function getExt3 ($ file) {return array_pop (explode ('. ', $ file);} // class 4, pathinfo function getExt5 ($ file) {$ arr = pathinfo ($ file); return $ arr ['extension']; // or return pathinfo ($ file, PATHINFO_EXTENSION);} // The fifth, regular, sub-mode function getExt6 $ file) {preg_match ("/(gif | jpg | png) $/", $ file, $ match); $ match = $ match [0];} // Type 6: Regular expression reverse reference function getExt7 ($ file) {$ match = preg_replace ("/. *\. (\ w +)/"," \ 1 ", $ file); echo $ match ;}
The above section summarizes the details of the PHP instance code for obtaining the file extension. For more information, see other related articles in the first PHP community!