PHP implementation method to get the file name extension

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

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.