search
HomeBackend DevelopmentPHP ProblemHow to get the file name without suffix in php

Obtaining method: 1. Use the basename() function, the syntax is "basename (file path, the suffix name that needs to be removed)"; 2. Use the pathinfo() function, the syntax is "pathinfo (file path, PATHINFO_FILENAME)" .

How to get the file name without suffix in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php is obtained without Methods for suffixed file names

1. Use the basename() function

The basename() function returns the file name part of the path. Syntax:

basename(path,suffix)
  • Parameter path: Indicates the path to be checked.

  • Parameter suffix: can be omitted, indicating the file extension. If the suffix parameter is not omitted, the file name without extension is output.

Example:

<?php
$path = "/testweb/home.php";

//显示带有文件扩展名的文件名
echo basename($path);

//显示不带有文件扩展名的文件名
echo basename($path,".php");
?>

Output:

home.php
home

2. Use the pathinfo() function

pathinfo () function returns information about the file path in the form of an array. Syntax:

pathinfo(path,options)
  • Parameter path: Indicates the path to be checked.

  • Parameter options: can be omitted, indicating the array elements to be returned, the default value is all. Can have the following values:

    • PATHINFO_DIRNAME: Only the directory name (dirname) is returned.

    • PATHINFO_BASENAME: Returns the complete file name (basename), that is, the file name with extension.

    • PATHINFO_EXTENSION: Returns only the extension (extension)

    • PATHINFO_FILENAME: Returns the file name (filename) without the extension.

Example:

<?php 
// 用文件名初始化变量
$file = &#39;demo.html&#39;; 
// 仅提取文件名
$x = pathinfo($file, PATHINFO_FILENAME); 
// 输出
echo $x; 
?>

Output:

demo

Recommended learning: "PHP Video Tutorial

The above is the detailed content of How to get the file name without suffix in php. For more information, please follow other related articles on the PHP Chinese website!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use