created StringHelper::basename()

In contrast to php function basename() It that always uses \ and /
as directory separator.
It is needed for use on Classnames that contain namespaces.
Fixes #192
This commit is contained in:
Carsten Brandt
2013-05-09 13:13:16 +02:00
parent 849740e6cc
commit 776162b05e
3 changed files with 68 additions and 1 deletions

View File

@ -43,6 +43,29 @@ class StringHelper
return function_exists('mb_substr') ? mb_substr($string, $start, $length, '8bit') : substr($string, $start, $length);
}
/**
* Returns the trailing name component of a path.
* This method does the same as the php function basename() except that it will
* always use \ and / as directory separators, independent of the operating system.
* Note: basename() operates naively on the input string, and is not aware of the
* actual filesystem, or path components such as "..".
* @param string $path A path string.
* @param string $suffix If the name component ends in suffix this will also be cut off.
* @return string the trailing name component of the given path.
* @see http://www.php.net/manual/en/function.basename.php
*/
public static function basename($path, $suffix = '')
{
if (($len = mb_strlen($suffix)) > 0 && mb_substr($path, -$len) == $suffix) {
$path = mb_substr($path, 0, -$len);
}
$path = rtrim(str_replace('\\', '/', $path), '/\\');
if (($pos = mb_strrpos($path, '/')) !== false) {
return mb_substr($path, $pos + 1);
}
return $path;
}
/**
* Converts a word to its plural form.
* Note that this is for English only!