wp_basename()WP 3.1.0

Gets the last component from the specified path or URL, retrieving everything after the last /.

This is a copy of the PHP function basename(), only it is localization-friendly (l18n).

It is recommended to use when obtaining file names passed via GET, POST parameters, or retrieved from the database. In other cases, use basename() - it is slightly faster.

1 time — 0.000025 sec (very fast) | 50000 times — 0.30 sec (very fast) | PHP 7.0.8, WP 4.6

No Hooks.

Returns

String. The name of the file or folder from the specified path or URL.

Usage

wp_basename( $path, $suffix );
$path(string) (required)
The path or URL or something else separated by /.
$suffix(string)
A string at the end (suffix) that should also be removed. For example, if you specify .jpg, then instead of image.jpg it will return image.
Default: ''

Examples

0

#1 Demonstration of work

echo wp_basename('http://domain/image.jpg', '.jpg'); //> image.jpg
echo wp_basename('/etc/image.jpg', '.jpg'); //> image
echo wp_basename('/etc/image.jpg');         //> image.jpg
echo wp_basename('/etc/passwd');            //> passwd
echo wp_basename('/etc/');                  //> etc
echo wp_basename('.');                      //> .
echo wp_basename('/'); //> empty string

// other encoding
echo wp_basename('/спiдма'); //> спiдма

Changelog

Since 3.1.0 Introduced.

wp_basename() code WP 6.9

function wp_basename( $path, $suffix = '' ) {
	return urldecode( basename( str_replace( array( '%2F', '%5C' ), '/', urlencode( $path ) ), $suffix ) );
}