This article demonstrates how to extract file names without file extensions in PHP.

1. Using pathinfo() function

The pathinfo() function returns all the components of your path. It returns an associative array containing dirname, basename, extension, and filename information.

Download Code

 
You may also specify a flag to the pathinfo() function to retrieve path information for a single component. This flag can be one of PATHINFO_DIRNAME, PATHINFO_BASENAME, PATHINFO_EXTENSION or PATHINFO_FILENAME. To extract the filename component of the path, you can use pathinfo() with the PATHINFO_FILENAME flag.

Download Code

2. Using basename() function

You may also use the basename() function, which returns the base name of the given path. It optionally takes a suffix, which will be removed from the result if the name component ends with it. For example,

Download Code

3. Using preg_replace() function

Another alternative is to use regular expressions for this task. In PHP, you can use the preg_replace() function, as demonstrated below:

Download Code

That’s all there is to extracting file names without file extensions in PHP.