Home > Backend Development > PHP Tutorial > How to Extract a Filename Without Its Extension in PHP?

How to Extract a Filename Without Its Extension in PHP?

Barbara Streisand
Release: 2024-11-11 18:03:03
Original
413 people have browsed it

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:

  1. Using __FILE__ Constant:
$filename = basename(__FILE__, '.php');
Copy after login
  1. Generic File Extension Remover Function:
function chopExtension($filename) {
    return pathinfo($filename, PATHINFO_FILENAME);
}
Copy after login
  1. Using Standard String Library Functions:
function chopExtension($filename) {
    return substr($filename, 0, strrpos($filename, '.'));
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template