Sample code for PHP to get the file name extension

Source: Internet
Author: User
The file extension (filename extension), also known as the suffix name of a file, is a mechanism used by the operating system to flag file types. Typically, an extension is followed by the main file name, separated by a delimiter. In a file name like "Read Me. txt", read I is the main file name, txt is the extension (text, foreign language full name: text), indicating that this file is considered a plain text file. The extension can be thought of as a type of meta-data,

Some file systems limit the length of extensions, such as the PC disk operating system (Pc-dos) and the FAT file system on the Microsoft Disk operating system (MS-DOS) do not allow more than three characters, IBM VM/CMS does not allow more than eight characters, while others such as NTFS "New Technology File System" is not limited, while the UNIX operating system's file system accepts the delimiter point as a valid character.

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 following code

 $file = ' files that need to get the extension. php ';//The first, according to. Split to get the value of the last element function Getext1{return end ( Explode (".", $file);)}//Second, get 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 Article

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: 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.