What are the three ways to get the file extension in php

青灯夜游
Release: 2023-03-11 11:58:01
Original
13454 people have browsed it

Obtaining method: 1. Use the "array_pop(explode('.', file name))" statement; 2. Use the "pathinfo (file name) ['extension']" statement; 3. Use "strrev (strchr(strrev(filename),'.',true))" statement.

What are the three ways to get the file extension in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php gets the file suffix Name method 1:

Copy after login

Output:

jpg
Copy after login
Copy after login
Copy after login

Description:

explode() function uses a string split Another string and returns an array of strings.

array_pop() function deletes the last element in the array.

php method 2 to get the file suffix name:

Copy after login

Output:

jpg
Copy after login
Copy after login
Copy after login

Description:

pathinfo() function returns information about the file path in the form of an array.

The returned array elements are as follows:

  • [dirname]: directory path

  • [basename]: file name

  • [extension]: File suffix name

  • [filename]: File name without suffix

php method 3 to get the file suffix name:

Copy after login

Output:

jpg
Copy after login
Copy after login
Copy after login

Description:

strrev() function reverses a string.

strchr() function searches for the first occurrence of a string in another string. Syntax:

strchr(string,search,before_search);
Copy after login
Parameter Description
string Required. Specifies the string to be searched for.
search Required. Specifies the string to search for. If the argument is a number, searches for characters that match the ASCII value for that number.
before_search Optional. A Boolean value with a default value of "false". If set to "true", it will return the portion of the string preceding the first occurrence of thesearchparameter.

Return value: Returns the rest of the string (from the matching point). Returns FALSE if the searched string is not found.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What are the three ways to get the file extension in php. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!