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.                                       
Get Solution for free from DataCamp guru