Skip to content

Commit

Permalink
Added StringHelper::dirname()
Browse files Browse the repository at this point in the history
  • Loading branch information
qiangxue committed Sep 10, 2013
1 parent 347e79a commit e9a5b92
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions framework/yii/helpers/StringHelperBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public static function 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.
* This method is similar to the php function `basename()` except that it will
* 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
* 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 "..".
Expand All @@ -69,6 +69,24 @@ public static function basename($path, $suffix = '')
return $path;
}

/**
* 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.
* This is a wrapper of the [phpspec/php-diff](https://packagist.org/packages/phpspec/php-diff) package.
Expand Down

0 comments on commit e9a5b92

Please sign in to comment.