Remove file extension from filename in PHP

I found great function to remove file extension from filename in PHP.  Anton Zamov created this function and I’m just reporting it to show my appreciation.

Function:


function RemoveExtension($strName) 
{    
    $ext = strrchr($strName, '.');    
    if($ext !== false) 
    {       
        $strName = substr($strName, 0, -strlen($ext));    
    }    
    return $strName; 
}

Usage:

$filename = "myfile.jpeg"; echo RemoveExtension($filename);

Output:

myfile

Short, easy and understandable. If someone has a better solution let me know.

4 thoughts on “Remove file extension from filename in PHP

  1. You can use the inbuilt pathinfo function to access the filename without extension

    $filename= ‘woof.jpg’;
    $string=pathinfo($filename,PATHINFO_FILENAME);
    echo $filename;

    // outputs woof

  2. $filename= ‘woof.jpg’;
    $string=strstr($filename,’.’,true);
    echo $filename;

    // outputs woof

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.




Recent Posts

GiottoPress by Enrique Chavez