Home Backend Development PHP Tutorial PHP get file extension instance parsing

PHP get file extension instance parsing

Jun 09, 2020 am 09:16 AM
file extension

PHP get file extension instance parsing

PHP Get File Extension Example

First:

$file = 'x.y.z.png';
echo substr(strrchr($file, '.'), 1);
Copy after login

Parsing: strrchr ($file, '.')

strrchr() function finds the last occurrence of a string in another string and returns all characters from that position to the end of the string

Second type:

$file = 'x.y.z.png';
echo substr($file, strrpos($file, '.')+1);
Copy after login

Analysis: strrpos($file, '.')

Find the last occurrence of "." in the string, and return the position substr() from that Position start interception

The third type:

$file = 'x.y.z.png';
$arr = explode('.', $file);
echo $arr[count($arr)-1];
Copy after login

The fourth type:

$file = 'x.y.z.png';
$arr = explode('.', $file);
echo end($arr);  //end()返回数组的最后一个元素
Copy after login

The fifth type:

$file = 'x.y.z.png';
echo strrev(explode('.', strrev($file))[0]);
Copy after login

Sixth type:

.$file = 'x.y.z.png';
echo pathinfo($file)['extension'];
Copy after login

Analysis: The pathinfo() function returns the file path information in the form of an array.

Includes the following array elements:

[dirname]
[basename]
[extension]
Copy after login

The seventh type:

.$file = 'x.y.z.png';
echo pathinfo($file, PATHINFO_EXTENSION);
Copy after login

Summary: I personally prefer the seventh one Kind of

Recommended tutorial: "PHP Video Tutorial"

The above is the detailed content of PHP get file extension instance parsing. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to change file extension on iPhone How to change file extension on iPhone Mar 04, 2024 pm 04:34 PM

On iOS devices, the AppleFiles app allows you to change the file extension of files saved on your iPhone. Changing file extensions is not generally recommended, but this feature may be useful to ensure files are compatible with a specific application, or to intentionally hide files. In this article, we will explain how to change the file extension of files saved on iPhone. How to change file extensions on iPhone Requirements: iPhone running iOS16 or later Open the Files app on your iPhone and navigate to the desired location. Press and hold the file whose file extension you want to change. Click Rename. You can now remove the existing file extension and type a new file extension for this file.

How to get file extension in Python? How to get file extension in Python? Sep 08, 2023 pm 01:53 PM

A file extension in Python is a suffix appended to the end of a file name to indicate the format or type of the file. It usually consists of three or four characters, a file name followed by a period, such as ".txt" or ".py". Operating systems and programs use file extensions to determine what type of file it is and how it should be processed. Recognized as a plain text file. File extensions in Python are crucial when reading or writing files because it establishes the file format and the best way to read and write data. For example, the ".csv" file extension is the extension used when reading CSV files, and the csv module is used to process the files. Algorithm for obtaining file extension in Python. Manipulate file name string in Python.

What does file extension mean What does file extension mean Jul 29, 2022 am 11:15 AM

File extension is a mechanism used by the operating system to mark file types; the main function of file extension is to let the system decide what software should be used to run when the user opens the specified file, so it can be used to protect certain files, Save disk space, clear icons on settings panel, and set associations.

How to search a directory with a specific file extension in Java? How to search a directory with a specific file extension in Java? Aug 31, 2023 am 08:13 AM

The following example prints files in a directory based on their extension - Example importjava.io.IOException;importjava.nio.file.Files;importjava.nio.file.Path;importjava.nio.file.Paths;importjava.util.stream.Stream; publicclassDemo{ publicstaticvoidmain(String[]args)throwsIOException{&nbsp

How to show file extensions in Windows 11 How to show file extensions in Windows 11 Jan 29, 2024 pm 11:12 PM

How to display file extension in win11? The win11 system hides file extensions. I believe many users want to display the file extensions. So how to set this? Let this site carefully introduce to users how to display win11 file extensions. Introduction to how to display file extensions in win11 Method 1: 1. Double-click to enter This Computer on the desktop, and click the View tab in the toolbar at the top of the page. Method 2: 1. Click the search button in the taskbar and enter the Folder Explorer option in the search box to open it.

PHP regular expression method to verify file extension PHP regular expression method to verify file extension Jun 24, 2023 pm 12:13 PM

In the process of web development, it is sometimes necessary to impose format restrictions on uploaded files, that is, only files of specified types can be uploaded to pass verification. At this time, you can use PHP regular expressions to verify the file extension. PHP regular expression is a powerful tool for matching and searching strings. It can help us quickly and accurately determine whether a piece of text meets the requirements according to certain rules and further process it. Next, this article will explain how to use PHP regular expressions for file extension verification. first step:

What does a file extension usually mean? What does a file extension usually mean? Aug 15, 2023 am 10:49 AM

A file extension is a string of characters used to identify the file type. It usually consists of one or more letters that follow the file name and are separated by a dot. It tells the operating system and applications how to handle it. and interpret the document.

What can be used to unambiguously represent unique files in a folder? What can be used to unambiguously represent unique files in a folder? Aug 01, 2022 pm 01:43 PM

A unique file within a folder can be clearly identified via a "file name". The file name is an identifier of the file's existence, and the operating system controls and manages it based on the file name; in order to facilitate people to distinguish different files in the computer, each file needs to be given a designated name. Files in the same folder have unique file names and cannot be repeated; but file names in different folders can be the same.

See all articles