Commit e9a5b92d by Qiang Xue

Added StringHelper::dirname()

parent 347e79a4
...@@ -47,8 +47,8 @@ class StringHelperBase ...@@ -47,8 +47,8 @@ class StringHelperBase
/** /**
* Returns the trailing name component of a path. * Returns the trailing name component of a path.
* This method does the same as the php function `basename()` except that it will * This method is similar to the php function `basename()` except that it will
* always use \ and / as directory separators, independent of the operating system. * treat both \ and / as directory separators, independent of the operating system.
* This method was mainly created to work on php namespaces. When working with real * This method was mainly created to work on php namespaces. When working with real
* file paths, php's `basename()` should work fine for you. * file paths, php's `basename()` should work fine for you.
* Note: this method is not aware of the actual filesystem, or path components such as "..". * Note: this method is not aware of the actual filesystem, or path components such as "..".
...@@ -70,6 +70,24 @@ class StringHelperBase ...@@ -70,6 +70,24 @@ class StringHelperBase
} }
/** /**
* Returns parent directory's path.
* This method is similar to `dirname()` except that it will treat
* both \ and / as directory separators, independent of the operating system.
* @param string $path A path string.
* @return string the parent directory's path.
* @see http://www.php.net/manual/en/function.basename.php
*/
public static function dirname($path)
{
$pos = mb_strrpos(str_replace('\\', '/', $path), '/');
if ($pos !== false) {
return mb_substr($path, 0, $pos);
} else {
return $path;
}
}
/**
* Compares two strings or string arrays, and return their differences. * Compares two strings or string arrays, and return their differences.
* This is a wrapper of the [phpspec/php-diff](https://packagist.org/packages/phpspec/php-diff) package. * This is a wrapper of the [phpspec/php-diff](https://packagist.org/packages/phpspec/php-diff) package.
* @param string|array $lines1 the first string or string array to be compared. If it is a string, * @param string|array $lines1 the first string or string array to be compared. If it is a string,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment