wp_basename()WP 3.1.0

i18n friendly version of basename()

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

No Hooks.

Return

String.

Usage

wp_basename( $path, $suffix );
$path(string) (required)
A path.
$suffix(string)
If the filename ends in suffix this will also be cut off.
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.

Code of wp_basename() WP 5.9.3

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