Extracting the Filename Without Extension in PHP
Getting the filename of the currently executed script in PHP is easy with the magic constant __FILE__. However, if you need to extract the filename without its extension, such as the ".php" suffix, the process is slightly different.
The basename() Solution:
To remove the extension using the basename() function, you can:
basename(__FILE__, '.php');
This will return the filename without the .php extension, such as "jquery.js" for the string "jquery.js.php".
A Generic Extension Remover:
For a more versatile solution that can handle any file extension, you can define a custom function:
function chopExtension($filename) { return pathinfo($filename, PATHINFO_FILENAME); }
Using this function:
var_dump(chopExtension('bob.php')); // "bob" var_dump(chopExtension('bob.i.have.dots.zip')); // "bob.i.have.dots"
Standard String Functions:
Finally, you can use standard string functions for a quicker approach:
function chopExtension($filename) { return substr($filename, 0, strrpos($filename, '.')); }
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3