Blog de programación, errores, soluciones

Chose Language:
Author: Admin/Publisher |finished | checked

PHP basename function

PHP basename function returns the name of the last element in the given path. The suffix option allows us to remove any suffix it has.

Description / Descripción
basename(string $path, string $suffix = ""): string

Note: basename() operates directly on the input string (the string itself), and it is not aware of the file system or path components, such as "..".

In other words, basename() directly operates on the string. If we pass a value like "..", basename() has no idea of what exists in that directory and will return "..".

basename() depends on the locale setting. Therefore, to obtain the correct base name in paths that contain multibyte characters, the corresponding locale must be set using the setlocale() function.

If the path contains characters that are not valid for the current locale, the behavior of basename() is undefined.


$path – the path

In Windows , both / and \ are used as directory separators. However, in other environments like Linux , only / is used as the directory separator.

$suffix – If the returned component name ends with a suffix, it will be removed.


It returns the base name of the given path (ruta).

A example
echo "1) ".basename("/etc/sudoers.d", ".d").PHP_EOL;
echo "2) ".basename("/etc/sudoers.d").PHP_EOL;
echo "3) ".basename("/etc/passwd").PHP_EOL;
echo "4) ".basename("/etc/").PHP_EOL;
echo "5) ".basename(".").PHP_EOL;
echo "6) ".basename("/");
1) sudoers
2) sudoers.d
3) passwd
4) etc
5) .
Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category