Home > Article > Backend Development > How to Extract a Filename Without Its Extension in PHP?
Getting Filename Without Extension in PHP
Problem:
When working with PHP scripts, you may need to access the filename of the currently executed script. However, this filename typically includes the ".php" extension. How can you extract just the filename without this extension?
Answer:
To obtain the current filename with its extension, you can use the magic constant __FILE__ in PHP. However, if you desire to exclude the ".php" extension from the result:
$filename = basename(__FILE__, '.php');
function chopExtension($filename) { return pathinfo($filename, PATHINFO_FILENAME); }
function chopExtension($filename) { return substr($filename, 0, strrpos($filename, '.')); }
These functions can be applied to filenames with any extension, not just ".php" files.
The above is the detailed content of How to Extract a Filename Without Its Extension in PHP?. For more information, please follow other related articles on the PHP Chinese website!