Summary PHP instance code for getting File extensions-php Tutorial

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.