/Source/Classes/Core/String.php

https://github.com/fanboyJD/styx · PHP · 290 lines · 103 code · 41 blank · 146 comment · 20 complexity · 8b3d909ac6a626a6c64a8f2472d646d4 MD5 · raw file

  1. <?php
  2. /**
  3. * Styx::String - Acts as a wrapper to String methods. Either uses the mbstring extension
  4. * or falls back to the native php functions.
  5. *
  6. * @package Styx
  7. * @subpackage Core
  8. *
  9. * @license MIT-style License
  10. * @author Christoph Pojer <christoph.pojer@gmail.com>
  11. */
  12. final class String {
  13. /**
  14. * Contains a list of the used string functions (either native or prefixed with mb_)
  15. *
  16. * @var array
  17. */
  18. public static $Fn = array();
  19. /**
  20. * Holds information about the currently installed php extensions related
  21. * to this Class
  22. *
  23. * @var array
  24. */
  25. public static $Features = array(
  26. 'mbstring' => true,
  27. 'iconv' => true,
  28. );
  29. private function __construct(){}
  30. private function __clone(){}
  31. /**
  32. * Initializes the String class and decides whether to use mbstring or native php string functions
  33. * based on the feature settings passed in
  34. *
  35. * @param array $features
  36. */
  37. public static function initialize($features){
  38. foreach(self::$Features as $k => $v)
  39. self::$Features[$k] = empty($features['feature.'.$k]) ? false : !!$features['feature.'.$k];
  40. foreach(array(
  41. 'strlen', 'strrpos', 'strpos', 'strtoupper', 'strtolower',
  42. 'substr', 'substr_count', 'stripos', 'strripos',
  43. ) as $v)
  44. self::$Fn[$v] = (self::$Features['mbstring'] ? 'mb_' : '').$v;
  45. if(self::$Features['mbstring']) mb_internal_encoding('UTF-8');
  46. }
  47. /**
  48. * Checks if the string ends with a certain value
  49. *
  50. * @param string $string
  51. * @param string $look
  52. * @return bool
  53. */
  54. public static function ends($string, $look){
  55. $Fn = self::$Fn;
  56. return $Fn['strrpos']($string, $look)===$Fn['strlen']($string)-$Fn['strlen']($look);
  57. }
  58. /**
  59. * Checks if the string starts with a certain value
  60. *
  61. * @param string $string
  62. * @param string $look
  63. * @return bool
  64. */
  65. public static function starts($string, $look){
  66. $Fn = self::$Fn;
  67. return $Fn['strpos']($string, $look)===0;
  68. }
  69. /**
  70. * Wrapper for strlen
  71. *
  72. * @see strlen
  73. * @param string $string
  74. * @return int
  75. */
  76. public static function length($string){
  77. $Fn = self::$Fn;
  78. return $Fn['strlen']((string)$string);
  79. }
  80. /**
  81. * Wrapper for strtoupper
  82. *
  83. * @see strtoupper
  84. * @param string $string
  85. * @return string
  86. */
  87. public static function toUpper($string){
  88. $Fn = self::$Fn;
  89. return $Fn['strtoupper']($string);
  90. }
  91. /**
  92. * Wrapper for strtolower
  93. *
  94. * @see strtolower
  95. * @param string $string
  96. * @return string
  97. */
  98. public static function toLower($string){
  99. $Fn = self::$Fn;
  100. return $Fn['strtolower']($string);
  101. }
  102. /**
  103. * Wrapper for strpos
  104. *
  105. * @see strpos
  106. * @param string $string
  107. * @param string $look
  108. * @param int $offset
  109. * @return bool|int
  110. */
  111. public static function pos($string, $look, $offset = null){
  112. $Fn = self::$Fn;
  113. return $Fn['strpos']($string, $look, pick($offset, 0));
  114. }
  115. /**
  116. * Wrapper for stripos
  117. *
  118. * @see stripos
  119. * @param string $string
  120. * @param string $look
  121. * @param int $offset
  122. * @return bool|int
  123. */
  124. public static function ipos($string, $look, $offset = null){
  125. $Fn = self::$Fn;
  126. return $Fn['stripos']($string, $look, pick($offset, 0));
  127. }
  128. /**
  129. * Wrapper for strrpos
  130. *
  131. * @see strrpos
  132. * @param string $string
  133. * @param string $look
  134. * @param int $offset
  135. * @return bool|int
  136. */
  137. public static function rpos($string, $look, $offset = null){
  138. $Fn = self::$Fn;
  139. return $Fn['strrpos']($string, $look, pick($offset, 0));
  140. }
  141. /**
  142. * Wrapper for strripos
  143. *
  144. * @see strripos
  145. * @param string $string
  146. * @param string $look
  147. * @param int $offset
  148. * @return bool|int
  149. */
  150. public static function ripos($string, $look, $offset = null){
  151. $Fn = self::$Fn;
  152. return $Fn['strripos']($string, $look, pick($offset, 0));
  153. }
  154. /**
  155. * Wrapper for substr
  156. *
  157. * @see substr
  158. * @param string $string
  159. * @param int $start
  160. * @param int $length
  161. * @return string
  162. */
  163. public static function sub($string, $start, $length = null){
  164. $Fn = self::$Fn;
  165. if($length) return $Fn['substr']($string, $start, $length);
  166. return $Fn['substr']($string, $start);
  167. }
  168. /**
  169. * Wrapper for substr_count
  170. *
  171. * @see substr_count
  172. * @param string $string
  173. * @param string $look
  174. * @return int
  175. */
  176. public static function subcount($string, $look){
  177. $Fn = self::$Fn;
  178. return $Fn['substr_count']($string, $look);
  179. }
  180. /**
  181. * Wrapper for ucfirst
  182. *
  183. * @see ucfirst
  184. * @param string $string
  185. * @return string
  186. */
  187. public static function ucfirst($string){
  188. if(!self::$Features['mbstring']) return ucfirst($string);
  189. $Fn = self::$Fn;
  190. return $Fn['strtoupper']($Fn['substr']($string, 0, 1)).$Fn['strtolower']($Fn['substr']($string, 1));
  191. }
  192. /**
  193. * Wrapper for str_replace
  194. *
  195. * @see str_replace
  196. * @param mixed $search
  197. * @param mixed $replace
  198. * @param string $string
  199. * @param int $count
  200. * @return string
  201. */
  202. public static function replace($search, $replace, $subject, $count = null){
  203. return str_replace($search, $replace, $subject, $count);
  204. }
  205. /**
  206. * Removes unnecessary whitespaces. If an array is passed it recursively cleans it,
  207. * typecasts floats and integers accordingly and unsets empty values
  208. *
  209. * @param mixed $string
  210. * @param mixed $whitespaces
  211. * @return mixed
  212. */
  213. public static function clean($string, $options = array()){
  214. if(is_scalar($options)) $options = array('whitespace' => $options);
  215. if(!isset($options['whitespace'])) $options['whitespace'] = true;
  216. if(!isset($options['erase'])) $options['erase'] = true;
  217. if(is_array($string)){
  218. foreach($string as $k => &$v){
  219. $float = (float)$v;
  220. if($v==(string)$float) $v = $float;
  221. elseif($v==='0' || $v===0 || ctype_digit((string)$v)) $v = Data::id($v);
  222. elseif($options['erase'] && (!$v || (is_string($v) && !trim($v)))) unset($string[$k]);
  223. else $v = self::clean($v, $options);
  224. }
  225. }else{
  226. $string = trim($string);
  227. if($options['whitespace']) $string = self::replace(array("\r\n", "\n", "\t"), array(($options['whitespace']==='clean' ? " " : "\n"), ($options['whitespace']==='clean' ? " " : "\n"), ""), $string);
  228. }
  229. return $string;
  230. }
  231. /**
  232. * Removes all invalid UTF-8 bytes when iconv is available
  233. *
  234. * @param mixed $array
  235. * @return mixed
  236. */
  237. public static function convert($data){
  238. if(!self::$Features['iconv']) return $data;
  239. static $ini = false;
  240. if($ini===false) $ini = error_reporting(0);
  241. elseif($ini) error_reporting(0);
  242. if(is_array($data))
  243. array_walk_recursive($data, array('self', 'convert'));
  244. else
  245. $data = iconv('UTF-8', 'UTF-8//IGNORE', $data);
  246. if($ini) error_reporting($ini);
  247. return $data;
  248. }
  249. }