How to get the file name without suffix in php

青灯夜游
Release: 2023-03-13 07:16:01
Original
3970 people have browsed it

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)
Copy after login
  • 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");
?>
Copy after login

Output:

home.php
home
Copy after login

2. Use the pathinfo() function

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

pathinfo(path,options)
Copy after login
  • 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; 
?>
Copy after login

Output:

demo
Copy after login

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!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template