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]
##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)
Code example:
<?php // 用文件名初始化变量 $file = 'demo.html'; // 仅提取文件名 $x = pathinfo($file, PATHINFO_FILENAME); // 输出 echo $x; ?>
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)
Code example:
<?php $file = 'demo/filename.txt'; $x = basename($file, '.txt'); echo $x; ?>
filename
Method 3: Use the substr() and strrpos() functions
Code example:
<?php $file = 'PHP.pdf'; $x = substr($file, 0, strrpos($file, '.')); echo $x; ?>
PHP
demo/PHP
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!