Gordonmac Dot Com

Mostly a web development blog

Removing file extensions from a string with PHP

Posted: October 18th, 2006 | Tags: | Posted in: PHP, Tutorials
Note: This tutorial was originally published in 2006. The tips and techniques explained may be outdated.

This is a question, and I ask because I need some help :)

I have been trying to figure out the best way to remove file extensions from a string, but being completely crap at regular expressions (that’s no understatement by the way) the best I could come up with was something along the ugly lines of this:

function ridExtensions($thefile) {
  $ext = substr(strrchr($thefile, "."), 0);
  $thefile = str_replace($ext,'',$thefile);
  return $thefile;
}

Does anyone have a better method?