How to remove extension from string (only real extension!)
To remove the extension from a string in PHP, you can use the pathinfo()
function and the basename()
function.
Here is an example of how you can use these functions to remove the extension from a string:
<?php
$filename = "myfile.txt";
// Get the pathinfo of the file
$pathinfo = pathinfo($filename);
// Get the filename without the extension
$filename_without_extension = basename($filename, '.' . $pathinfo['extension']);
echo $filename_without_extension; // Outputs "myfile"
Watch a video course
Learn object oriented PHP
The pathinfo()
function returns an associative array that contains information about the path, such as the filename and the extension. The basename()
function returns the base name of the file (i.e., the filename without the directory path). By concatenating a period (.) and the extension from the pathinfo()
array, we can use the basename()
function to remove the extension from the filename.