Skip to content


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.

Posted in PHP.

Tagged with .


2 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Varun Nath says

    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. Nikola Vasiljevski Nikola Vasiljevski says

    Interesting, I have to try this out :) Thanks!



Some HTML is OK

or, reply to this post via trackback.