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)" .
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 = 'demo.html'; // 仅提取文件名 $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!