Home > Backend Development > PHP Tutorial > Summary of common methods for obtaining file extensions in PHP PHP skills

Summary of common methods for obtaining file extensions in PHP PHP skills

jacklove
Release: 2023-04-01 19:48:02
Original
1363 people have browsed it

This article mainly introduces the common methods of obtaining file extensions in PHP. It summarizes and analyzes five common operating techniques of obtaining file extensions in PHP in the form of examples. Friends in need can refer to the examples in this article.

Summarizes the common methods of obtaining file extensions in PHP. Share it with everyone for your reference, the details are as follows:

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

Use more than five methods to obtain the extension of a file.

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

Must use PHP's own processing function for processing, the method cannot be obviously repeated, and can be encapsulated into a function, For example, get_ext1($file_name), get_ext2($file_name)

The following are five methods I summarized by referring to online information. They are all relatively simple and do not say much. Say, go directly to the code:

Method 1:

function getExt1($filename)
{
   $arr = explode('.',$filename);
   return array_pop($arr);;
}
Copy after login

Method 2:

function getExt2($filename)
{
   $ext = strrchr($filename,'.');
   return $ext;
}
Copy after login

Method 3:

function getExt3($filename)
{
   $pos = strrpos($filename, '.');
   $ext = substr($filename, $pos);
   return $ext;
}
Copy after login

Method 4:

function getExt4($filename)
{
   $arr = pathinfo($filename);
   $ext = $arr['extension'];
   return $ext;
}
Copy after login

Method 5:

function getExt5($filename)
{
   $str = strrev($filename);
   return strrev(strchr($str,'.',true));
}
Copy after login

Articles you may be interested in:

PHP uses Curl to implement simulated login and data capture functions Example PHP skills

360 Search engine automatically includes php rewriting solution php examples

Detailed explanation of php and ethereum client interaction php examples

The above is the detailed content of Summary of common methods for obtaining file extensions in PHP PHP skills. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template