Change language

How to remove an extension from a string in PHP?

There are three ways to remove an extension from a string. They are as follows
  • Using the pathinfo intrinsic function
  • Using the basename intrinsic function
  • Using the string substr and strrpos functions
Using the function pathinfo(): The pathinfo() function returns an array containing the directory name, base name , extension and file name.Syntax: pathinfo ($path, $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME) Alternatively, if only one PATHINFO_ constant is passed as a parameter, only that part of the full filename is returned.Example:  
// Initialize the variable data with the file name $file = ’filename.html’ ;  
// Extract filename only using constants $x = pathinfo ( $file , PATHINFO_FILENAME);  
// Print result echo $x ;  
filename  Note.If the filename contains a full path, only the filename without the extension is returned.Using the  basename(): The basename() function is used to return the trailing component of a pathname as a string. Basename() works naively on the input string and is unaware of the actual filesystem or path components such as ".." 
basename ($path, $suffix)
When a file extension is known, it can be passed as a parameter to the basename function to tell it to remove that extension from the file name.Example:  
// Initialize the variable
// with the file name $file = ’filename.txt’ ;  
// The suffix is ​​passed as the second
// parameter $x = basename ( $file , ’.txt’ );  
// Print result echo $x ;  
filename  Using the function  substr()  and  strrpos() .Another way to remove the extension from the filename - using the string functions substr and strrpos. Substr() returns a portion of a string, while strrpos() finds the position of the last occurrence of a substring in a string.Syntax:
substr ($string, $start, $length)
Example :  
// Variable initialization
/ / with file name $file = ’filename.txt’ ;  
// Using substr $x = substr ( $file , 0, strrpos ( $file , ’.’ ));  
// Show file name echo $x ;  
filename  Note.If the file name contains a full path, then the full path and file name without extension is returned.



Best laptop for Sims 4


Best laptop for Zoom


Best laptop for Minecraft


Best laptop for engineering student


Best laptop for development


Best laptop for Cricut Maker


Best laptop for hacking


Best laptop for Machine Learning


Latest questions


psycopg2: insert multiple rows with one query

12 answers


How to convert Nonetype to int or string?

12 answers


How to specify multiple return types using type-hints

12 answers


Javascript Error: IPython is not defined in JupyterLab

12 answers


Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method