Detailed explanation of PHP basename() function usage

王林
Release: 2023-06-27 15:26:01
Original
2663 people have browsed it

The basename() function in PHP is a very useful function used to obtain the file name part of the specified path. Here is a detailed introduction to the usage of the PHP basename() function.

1. Function prototype

basename (string $path, string $suffix): string

2. Function parameters

$path: file needs to be obtained The path string of the name. Can be a relative path or an absolute path.

$suffix: Optional, if the file name ends with $suffix, this part will be removed.

3. Function return value

The return value of the basename() function is the file name under the specified path. If there is a $suffix parameter, delete the part ending with $suffix in the file name.

4. Function Example

In the following example, we will use the following path:/var/www/html/test.txt

  1. Get the file name

echo basename('/var/www/html/test.txt'); // Output: test.txt

  1. Remove file extension

echo basename('/var/www/html/test.txt', '.txt'); // Output: test

  1. Get the file name under the current path

echo basename(__FILE__); // Output: basename.php

  1. Get the folder name

$dir = dirname('/var /www/html/test.txt');
echo basename($dir); // Output: html

5. Notes

  1. If the path starts with an oblique If the string ends with a dash (/), an empty string is returned. For example:

echo basename('/var/www/html/'); // Output: ''

  1. If the path is "." or ".. ", the corresponding file name string is returned.

echo basename('.'); // Output: '.'
echo basename('..'); // Output: '..'

  1. The $suffix parameter is case-sensitive. For example:

echo basename('/var/www/html/test.TXT', '.txt'); // Output: test.TXT

  1. if Returns false if the given path is not a folder or file.

echo basename('/var/www/html/test'); // Output: test

The above is the detailed explanation of the PHP basename() function. Through this function, we can easily obtain the file name or folder name and perform corresponding operations. In the actual development process, we can also use it in combination with other functions and variables as needed to achieve more flexible and efficient results.

The above is the detailed content of Detailed explanation of PHP basename() function usage. For more information, please follow other related articles on the PHP Chinese website!

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