StringHelperBase.php 2.44 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

8
namespace yii\helpers;
Qiang Xue committed
9 10

/**
11 12 13
 * StringHelperBase provides concrete implementation for [[StringHelper]].
 *
 * Do not use StringHelperBase. Use [[StringHelper]] instead.
Qiang Xue committed
14 15 16 17 18
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @author Alex Makarov <sam@rmcreative.ru>
 * @since 2.0
 */
19
class StringHelperBase
Qiang Xue committed
20 21 22
{
	/**
	 * Returns the number of bytes in the given string.
Qiang Xue committed
23
	 * This method ensures the string is treated as a byte array by using `mb_strlen()`.
Qiang Xue committed
24 25 26 27 28
	 * @param string $string the string being measured for length
	 * @return integer the number of bytes in the given string.
	 */
	public static function strlen($string)
	{
Qiang Xue committed
29
		return mb_strlen($string, '8bit');
Qiang Xue committed
30 31 32 33
	}

	/**
	 * Returns the portion of string specified by the start and length parameters.
Qiang Xue committed
34
	 * This method ensures the string is treated as a byte array by using `mb_substr()`.
Qiang Xue committed
35 36 37 38 39 40 41 42
	 * @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)
	{
Qiang Xue committed
43
		return mb_substr($string, $start, $length, '8bit');
Qiang Xue committed
44 45
	}

46 47
	/**
	 * Returns the trailing name component of a path.
48
	 * This method does the same as the php function `basename()` except that it will
49
	 * always use \ and / as directory separators, independent of the operating system.
50 51
	 * This method was mainly created to work on php namespaces. When working with real
	 * file paths, php's `basename()` should work fine for you.
Qiang Xue committed
52
	 * Note: this method is not aware of the actual filesystem, or path components such as "..".
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
	 * @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;
	}
Qiang Xue committed
69
}