How to Extract the Filename Without Extension in PHP?
Nov 09, 2024 am 06:30 AMExtracting 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, '.')); }
The above is the detailed content of How to Extract the Filename Without Extension in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel

6 Extra Skills Every PHP Developer Should Have

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon
