The basename Function
The basename
function returns the trailing name component of the path. The first parameter
is the path string, the second (optional) one is the suffix to be stripped from the result. The function works with both UNIX-style and Windows paths.
Syntax
basename(path, [suffix]);
Example
Getting a filename from a full path:
<?php
echo basename('/var/www/site/index.html');
?>
Code execution result:
'index.html'
Example
Getting a filename with the extension removed:
<?php
echo basename('/var/www/site/index.html', '.html');
?>
Code execution result:
'index'
Example
Working with Windows paths:
<?php
echo basename('C:\Windows\system32\cmd.exe');
?>
Code execution result:
'cmd.exe'
Example
Getting a directory name:
<?php
echo basename('/var/www/site/');
?>
Code execution result:
'site'