You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
2.4 KiB
70 lines
2.4 KiB
12 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
12 years ago
|
namespace yii\helpers;
|
||
12 years ago
|
|
||
|
/**
|
||
12 years ago
|
* StringHelperBase provides concrete implementation for [[StringHelper]].
|
||
|
*
|
||
|
* Do not use StringHelperBase. Use [[StringHelper]] instead.
|
||
12 years ago
|
*
|
||
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
|
* @author Alex Makarov <sam@rmcreative.ru>
|
||
|
* @since 2.0
|
||
|
*/
|
||
12 years ago
|
class StringHelperBase
|
||
12 years ago
|
{
|
||
|
/**
|
||
|
* Returns the number of bytes in the given string.
|
||
12 years ago
|
* This method ensures the string is treated as a byte array by using `mb_strlen()`.
|
||
12 years ago
|
* @param string $string the string being measured for length
|
||
|
* @return integer the number of bytes in the given string.
|
||
|
*/
|
||
|
public static function strlen($string)
|
||
|
{
|
||
12 years ago
|
return mb_strlen($string, '8bit');
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the portion of string specified by the start and length parameters.
|
||
12 years ago
|
* This method ensures the string is treated as a byte array by using `mb_substr()`.
|
||
12 years ago
|
* @param string $string the input string. Must be one character or longer.
|
||
|
* @param integer $start the starting position
|
||
|
* @param integer $length the desired portion length
|
||
|
* @return string the extracted part of string, or FALSE on failure or an empty string.
|
||
|
* @see http://www.php.net/manual/en/function.substr.php
|
||
|
*/
|
||
|
public static function substr($string, $start, $length)
|
||
|
{
|
||
12 years ago
|
return mb_substr($string, $start, $length, '8bit');
|
||
12 years ago
|
}
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Returns the trailing name component of a path.
|
||
12 years ago
|
* This method does the same as the php function `basename()` except that it will
|
||
12 years ago
|
* always use \ and / as directory separators, independent of the operating system.
|
||
12 years ago
|
* This method was mainly created to work on php namespaces. When working with real
|
||
|
* file paths, php's `basename()` should work fine for you.
|
||
12 years ago
|
* Note: this method is not aware of the actual filesystem, or path components such as "..".
|
||
12 years ago
|
* @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;
|
||
|
}
|
||
12 years ago
|
}
|