How to remove extension from a file name using PHP

This code can be used to remove an extension from the file name using php.

If manipulated abit, it can also be used to find the extension of php and echo it.

function RemoveExtension($strName)
$ext = strrchr($strName, ‘.’);

if($ext !== false)
$strName = substr($strName, 0, -strlen($ext));
return $strName;

$Name = ‘file.txt’;

echo RemoveExtension($filename);

However, in my case, i tried using it with a bunch of code but wasn’t successful, it did removed the extension of the file name but it caused the remaining code to stop executing. I didn’t work out on why it caused it as was busy in some more important part of that code.

Leave a Reply

Your email address will not be published.