Five ways PHP Gets the file name extension

Source: Internet
Author: User

This is a written question I encountered when I applied for an internship:

Use more than five ways to get the extension of a file.

Requirements: dir/upload.image.jpg, find. jpg or JPG,

Must be handled using PHP's own processing function, which cannot be significantly duplicated and can be encapsulated into functions such as GET_EXT1 ($file _name), get_ext2 ($file _name)

The following is my reference to the online summary of the five methods, are relatively simple, words not much to say, directly on the code:

Method 1:

function GetExt1 ($filename)

{

$arr = Explode ('. ', $filename);

Return Array_pop ($arr);;

}

Method 2:

function GetExt2 ($filename)

{

$ext = STRRCHR ($filename, '. ');

return $ext;

}

Method 3:

function GetExt3 ($filename)

{

$pos = Strrpos ($filename, '. ');

$ext = substr ($filename, $pos);

return $ext;

}

Method 4:

function GetExt4 ($filename)

{

$arr = PathInfo ($filename);

$ext = $arr [' extension '];

return $ext;

}

Method 5:

function GetExt5 ($filename)

{

$str = Strrev ($filename);

Return Strrev (STRCHR ($str, '. ', true));

}

Five ways PHP Gets the file name extension

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.