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ónbasename(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 ".."
.
Caution:
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.
Parameters
$path
– the path
/
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.
Returns
It returns the base name of the given path (ruta).
A php.net exampleecho "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) . 6)