Home >Backend Development >PHP Problem >How to get the file name without suffix in php

How to get the file name without suffix in php

青灯夜游
青灯夜游Original
2021-09-28 18:57:084040browse

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