1 file.inc | backdrop_basename($uri, $suffix = NULL) |
Gets the filename from a given path.
Similar to PHP's basename() with the following differences:
- Any trailing slashes are automatically trimmed.
- Is not locale-aware, and works with all UTF-8 characters. Where PHP might not process file paths with non-Latin characters unless allowed through setlocale(), this function will remove the extensions from any string.
See also
basename()
Related topics
File
- core/
includes/ file.inc, line 2616 - API for handling file uploads and server file management.
Code
function backdrop_basename($uri, $suffix = NULL) {
$separators = '/';
if (DIRECTORY_SEPARATOR != '/') {
// For Windows OS add special separator.
$separators .= DIRECTORY_SEPARATOR;
}
// Remove right-most slashes when $uri points to directory.
$uri = rtrim($uri, $separators);
// Returns the trailing part of the $uri starting after one of the directory
// separators.
$filename = preg_match('@[^' . preg_quote($separators, '@') . ']+$@', $uri, $matches) ? $matches[0] : '';
// Cuts off a suffix from the filename.
if ($suffix) {
$filename = preg_replace('@' . preg_quote($suffix, '@') . '$@', '', $filename);
}
return $filename;
}