Home  >  Article  >  Backend Development  >  How to get filename without extension using PHP? (code example)

How to get filename without extension using PHP? (code example)

青灯夜游
青灯夜游Original
2019-02-21 11:25:004419browse

How to get the file name without extension? The following article will introduce to you how to use PHP to remove the extension from the file name and return the file name without the extension. I hope it will be helpful to you. [Video tutorial recommendation: PHP tutorial]

How to get filename without extension using PHP? (code example)

##Method 1: Use the built-in function pathinfo()

The pathinfo() function will return the file path information in the form of an array, including: dirname, basename, extension, filename.

Basic 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 returns the directory name (dirname).

●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 without extension (filename).

Code example:

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

Output:

demo

Note: If the file name contains a full path, only no File name with extension.

Method 2: Use the built-in function basename()

The basename() function returns the file name part of the path, that is, without the extension The file name; this function is used to return the trailing name component of path as a string.

Basic 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.

Code example:

<?php 
$file = &#39;demo/filename.txt&#39;; 
$x = basename($file, &#39;.txt&#39;); 
echo $x; 
?>

Output:

filename

Method 3: Use the substr() and strrpos() functions

The substr() function is used to return a part of a string, while strrpos() finds the position of the last substring that appears in the string.

Code example:

<?php 
$file = &#39;PHP.pdf&#39;; 
$x = substr($file, 0, strrpos($file, &#39;.&#39;)); 
echo $x; 
?>

Output:

PHP

Note: If the file name contains a full path, the full path without extension and file name.

Example: If $file = 'demo/PHP.pdf', then return:

demo/PHP

The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to get filename without extension using PHP? (code example). 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