Menu Home

basename

osbo.com | PHP | Functions | basename

Returns trailing name component of path

Syntax

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

Parameters

path

A path. On Windows, both slash (/) and backslash (\) are used as directory separator character. In other environments, it is the forward slash (/).

suffix

If the name component ends in suffix this will also be cut off.

Return

Returns the base name of the given path.

Examples

path

<?

$path = "/directory/file.extension";

$return = basename($path);

echo $return;

?>
file.extension

suffix

<?

$path = "/directory/file.extension";
$suffix = ".extension";

$return = basename($path, $suffix);

echo $return;

?>
file

3

<?

echo basename("/directory/file.extension") . PHP_EOL;
echo basename("/directory/file") . PHP_EOL;
echo basename("/directory/..") . PHP_EOL;
echo basename("/directory/.") . PHP_EOL;
echo basename("/directory/") . PHP_EOL;
echo basename("/directory") . PHP_EOL;
echo basename("/..") . PHP_EOL;
echo basename("/.") . PHP_EOL;
echo basename("/") . PHP_EOL;
echo basename("..") . PHP_EOL;
echo basename(".");

?>
file.extension
file
..
.
directory
directory
..
.

..
.

PHP