Created
August 9, 2017 03:22
-
-
Save TonyGao/1b4be6a5b16e62d1bddf270788857632 to your computer and use it in GitHub Desktop.
StaticString
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Winterstrong\QQApiBundle\TypeClass; | |
use Winterstrong\QQApiBundle\TypeClass\ArrClass as Arr; | |
/** | |
* Class StaticString | |
* @package Winterstrong\QQApiBundle\TypeClass | |
* | |
* Copy from https://github.com/alecgorge/PHP-String-Class | |
*/ | |
class StaticString { | |
/* static methods wrapping multibyte */ | |
/** | |
* Wrapper for substr | |
*/ | |
public static function substr($string, $start, $length = null) { | |
if(StringClass::$multibyte) { | |
return new StringClass(mb_substr($string, $start, $length, StringClass::$multibyte_encoding)); | |
} | |
return new StringClass(substr($string, $start, $length)); | |
} | |
/** | |
* Equivelent of Javascript's String.substring | |
* @link http://www.w3schools.com/jsref/jsref_substring.asp | |
*/ | |
public static function substring($string, $start, $end) { | |
if(empty($length)) { | |
return self::substr($string, $start); | |
} | |
return self::substr($string, $end - $start); | |
} | |
public function charAt ($str, $point) { | |
return self::substr($str, $point, 1); | |
} | |
public function charCodeAt ($str, $point) { | |
return ord(self::substr($str, $point, 1)); | |
} | |
public static function concat() { | |
$args = func_get_args(); | |
$r = ""; | |
foreach($args as $arg) { | |
$r .= (string)$arg; | |
} | |
return $arg; | |
} | |
public static function fromCharCode($code) { | |
return chr($code); | |
} | |
public static function indexOf($haystack, $needle, $offset = 0) { | |
if(StringClass::$multibyte) { | |
return mb_strpos($haystack, $needle, $offset, StringClass::$multibyte_encoding); | |
} | |
return strpos($haystack, $needle, $offset); | |
} | |
public static function lastIndexOf($haystack, $needle, $offset = 0) { | |
if(StringClass::$multibyte) { | |
return mb_strrpos($haystack, $needle, $offset, StringClass::$multibyte_encoding); | |
} | |
return strrpos($haystack, $needle, $offset); | |
} | |
public static function match($haystack, $regex) { | |
preg_match_all($regex, $haystack, $matches, PREG_PATTERN_ORDER); | |
return new Arr($matches[0]); | |
} | |
public static function replace($haystack, $needle, $replace, $regex = false) { | |
if($regex) { | |
$r = preg_replace($needle, $replace, $haystack); | |
} | |
else { | |
if(StringClass::$multibyte) { | |
$r = mb_str_replace($needle, $replace, $haystack); | |
} | |
else { | |
$r = str_replace($needle, $replace, $haystack); | |
} | |
} | |
return new StringClass($r); | |
} | |
public static function strlen($string) { | |
if(StringClass::$multibyte) { | |
return mb_strlen($string, StringClass::$multibyte_encoding); | |
} | |
return strlen($string); | |
} | |
public static function slice($string, $start, $end = null) { | |
return self::substring($string, $start, $end); | |
} | |
public static function toLowerCase($string) { | |
if(StringClass::$multibyte) { | |
return new StringClass(mb_strtolower($string, StringClass::$multibyte_encoding)); | |
} | |
return new StringClass(strtolower($string)); | |
} | |
public static function toUpperCase($string) { | |
if(StringClass::$multibyte) { | |
return new StringClass(mb_strtoupper($string, StringClass::$multibyte_encoding)); | |
} | |
return new StringClass(strtoupper($string)); | |
} | |
public static function split($string, $at = '') { | |
if(empty($at)) { | |
if(StringClass::$multibyte) { | |
return new Arr(mb_str_split($string)); | |
} | |
return new Arr(str_split($string)); | |
} | |
return new Arr(explode($at, $string)); | |
} | |
/* end static wrapper methods */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment