How to Remove Extension From String In PHP?
Jul 20, 2021 . Admin
Hi Dev
Today, In this article i will explain you how to remove extension from string from PHP.so it is easy to use and implement remove file extension from string PHP.
So, Here i will give you full example of remove file extension from string PHP so basically follow my bellow all steps.
Syntax:pathinfo ( $path, $options = PATHINFO_DIRNAME|PATHINFO_BASENAME|PATHINFO_EXTENSION|PATHINFO_FILENAME )
Three way to remove an extension from string.
- Using an inbuilt function pathinfo
- Using an inbuilt function basename
- Using an string functions substr and strrpos
The pathinfo() function returns an array containing the directory name, basename, extension and filename.
If only one PATHINFO_ constants is passed as a parameter it returns only full filename.
Example:<?php // Initializing a variable with filename $file = 'index.html'; // Extracting only filename using constants $x = pathinfo($file, PATHINFO_FILENAME); // Printing the result echo $x; ?>Output
indexbasename()
In this method 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 )Example:
<?php // Initializing a variable with filename $file = 'index.html'; // Suffix is passed as second $x = basename($file, '.html'); // Printing the result echo $x; >Output
indexsubstr() 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 )
<?php // Initializing a variable // with filename $file = 'index.html'; // Using substr $x = substr($file, 0, strrpos($file, '.')); // Display the filename echo $x; ?>Output
index
I Hope It Will Help You...