How to remove extension from string in PHP?
There are three ways of removing an extension from string. They are as follows
- Using an inbuilt function pathinfo
- Using an inbuilt function basename
- Using an string functions substr and strrpos
Using pathinfo() Function: The pathinfo() function returns an array containing the directory name, basename, extension and filename.
Syntax:
pathinfo ( $path, $options = PATHINFO_DIRNAME|PATHINFO_BASENAME|PATHINFO_EXTENSION|PATHINFO_FILENAME )
Alternatively, if only one PATHINFO_ constants is passed as a parameter it returns only that part of the full filename.
Example:
<?php // Initializing a variable with filename $file = 'filename.html'; // Extracting only filename using constants $x = pathinfo($file, PATHINFO_FILENAME); // Printing the result echo $x; ?> |
filename
Note: If the filename contains a full path, then only the filename without the extension is returned.
Using basename() Function: The basename() function is used to return trailing name component of path in the form of string. The basename() operates naively on the input string, and is not aware of the actual file-system, or path components such as “..”
Syntax:
basename ( $path, $suffix )
When an extension of file is known it can be passed as a parameter to basename function to tell it to remove that extension from the filename.
Example:
<?php // Initializing a variable // with filename $file = 'filename.txt'; // Suffix is passed as second // parameter $x = basename($file, '.txt'); // Printing the result echo $x; ?> |
filename
Using substr()and strrpos() function: Another way of removing an extension from a filename is using the string functions substr and strrpos. The substr() function returns the part of string whereas strrpos() finds the position of last occurrence of substring in a string.
Syntax:
substr ( $string, $start, $length )
Example:
<?php // Initializing a variable // with filename $file = 'filename.txt'; // Using substr $x = substr($file, 0, strrpos($file, '.')); // Display the filename echo $x; ?> |
filename
Note: If the filename contains a full path, then the full path and filename without the extension is returned.
Recommended Posts:
- How to trim a file extension from string using JavaScript ?
- Remove new lines from string in PHP
- How to remove the first character of string in PHP?
- How to remove portion of a string after a certain character in PHP?
- How to remove all non-printable characters in a string in PHP?
- How to remove text from a string in JavaScript?
- How to remove line breaks from the string in PHP?
- How to remove all leading zeros in a string in PHP ?
- How to remove a character from string in JavaScript ?
- How to remove all classes that begin with a certain string in JavaScript?
- How to remove portion of a string after certain character in JavaScript ?
- How to remove all line breaks from a string using JavaScript?
- Kotlin extension function
- How to Install ImageMagick and Imagick PHP extension in Ubuntu ?
- OOPS | Generalization as extension and restriction using Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



