Commit df616847 by Qiang Xue

Refactored StringHelper.

parent 9445953b
...@@ -18,20 +18,18 @@ class StringHelper ...@@ -18,20 +18,18 @@ class StringHelper
{ {
/** /**
* Returns the number of bytes in the given string. * Returns the number of bytes in the given string.
* This method ensures the string is treated as a byte array. * This method ensures the string is treated as a byte array by using `mb_strlen()`.
* It will use `mb_strlen()` if it is available.
* @param string $string the string being measured for length * @param string $string the string being measured for length
* @return integer the number of bytes in the given string. * @return integer the number of bytes in the given string.
*/ */
public static function strlen($string) public static function strlen($string)
{ {
return function_exists('mb_strlen') ? mb_strlen($string, '8bit') : strlen($string); return mb_strlen($string, '8bit');
} }
/** /**
* Returns the portion of string specified by the start and length parameters. * Returns the portion of string specified by the start and length parameters.
* This method ensures the string is treated as a byte array. * This method ensures the string is treated as a byte array by using `mb_substr()`.
* It will use `mb_substr()` if it is available.
* @param string $string the input string. Must be one character or longer. * @param string $string the input string. Must be one character or longer.
* @param integer $start the starting position * @param integer $start the starting position
* @param integer $length the desired portion length * @param integer $length the desired portion length
...@@ -40,15 +38,14 @@ class StringHelper ...@@ -40,15 +38,14 @@ class StringHelper
*/ */
public static function substr($string, $start, $length) public static function substr($string, $start, $length)
{ {
return function_exists('mb_substr') ? mb_substr($string, $start, $length, '8bit') : substr($string, $start, $length); return mb_substr($string, $start, $length, '8bit');
} }
/** /**
* 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 does the same as the php function basename() except that it will
* always use \ and / as directory separators, independent of the operating system. * 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 * Note: this method is not aware of the actual filesystem, or path components such as "..".
* actual filesystem, or path components such as "..".
* @param string $path A path string. * @param string $path A path string.
* @param string $suffix If the name component ends in suffix this will also be cut off. * @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. * @return string the trailing name component of the given path.
......
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