W3docs

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.

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:

Example of how to remove extension from a string in PHP

<?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"

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Learn object oriented PHP</div>

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.