PageRenderTime 85ms CodeModel.GetById 48ms RepoModel.GetById 10ms app.codeStats 0ms

/fuel/category_tool/fuel/core/classes/str.php

https://github.com/connvoi/dev
PHP | 333 lines | 178 code | 38 blank | 117 comment | 9 complexity | 8b464f7cbe3c497d9998df5d30aba39b MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.0
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2012 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. /**
  14. * String handling with encoding support
  15. *
  16. * PHP needs to be compiled with --enable-mbstring
  17. * or a fallback without encoding support is used
  18. */
  19. class Str
  20. {
  21. /**
  22. * Truncates a string to the given length. It will optionally preserve
  23. * HTML tags if $is_html is set to true.
  24. *
  25. * @param string $string the string to truncate
  26. * @param int $limit the number of characters to truncate too
  27. * @param string $continuation the string to use to denote it was truncated
  28. * @param bool $is_html whether the string has HTML
  29. * @return string the truncated string
  30. */
  31. public static function truncate($string, $limit, $continuation = '...', $is_html = false)
  32. {
  33. $offset = 0;
  34. $tags = array();
  35. if ($is_html)
  36. {
  37. // Handle special characters.
  38. preg_match_all('/&[a-z]+;/i', strip_tags($string), $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
  39. foreach ($matches as $match)
  40. {
  41. if ($match[0][1] >= $limit)
  42. {
  43. break;
  44. }
  45. $limit += (static::length($match[0][0]) - 1);
  46. }
  47. // Handle all the html tags.
  48. preg_match_all('/<[^>]+>([^<]*)/', $string, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
  49. foreach ($matches as $match)
  50. {
  51. if($match[0][1] - $offset >= $limit)
  52. {
  53. break;
  54. }
  55. $tag = static::sub(strtok($match[0][0], " \t\n\r\0\x0B>"), 1);
  56. if($tag[0] != '/')
  57. {
  58. $tags[] = $tag;
  59. }
  60. elseif (end($tags) == static::sub($tag, 1))
  61. {
  62. array_pop($tags);
  63. }
  64. $offset += $match[1][1] - $match[0][1];
  65. }
  66. }
  67. $new_string = static::sub($string, 0, $limit = min(static::length($string), $limit + $offset));
  68. $new_string .= (static::length($string) > $limit ? $continuation : '');
  69. $new_string .= (count($tags = array_reverse($tags)) ? '</'.implode('></',$tags).'>' : '');
  70. return $new_string;
  71. }
  72. /**
  73. * Add's _1 to a string or increment the ending number to allow _2, _3, etc
  74. *
  75. * @param string $str required
  76. * @return string
  77. */
  78. public static function increment($str, $first = 1, $separator = '_')
  79. {
  80. preg_match('/(.+)'.$separator.'([0-9]+)$/', $str, $match);
  81. return isset($match[2]) ? $match[1].$separator.($match[2] + 1) : $str.$separator.$first;
  82. }
  83. /**
  84. * substr
  85. *
  86. * @param string $str required
  87. * @param int $start required
  88. * @param int|null $length
  89. * @param string $encoding default UTF-8
  90. * @return string
  91. */
  92. public static function sub($str, $start, $length = null, $encoding = null)
  93. {
  94. $encoding or $encoding = \Fuel::$encoding;
  95. // substr functions don't parse null correctly
  96. $length = is_null($length) ? (function_exists('mb_substr') ? mb_strlen($str, $encoding) : strlen($str)) - $start : $length;
  97. return function_exists('mb_substr')
  98. ? mb_substr($str, $start, $length, $encoding)
  99. : substr($str, $start, $length);
  100. }
  101. /**
  102. * strlen
  103. *
  104. * @param string $str required
  105. * @param string $encoding default UTF-8
  106. * @return int
  107. */
  108. public static function length($str, $encoding = null)
  109. {
  110. $encoding or $encoding = \Fuel::$encoding;
  111. return function_exists('mb_strlen')
  112. ? mb_strlen($str, $encoding)
  113. : strlen($str);
  114. }
  115. /**
  116. * lower
  117. *
  118. * @param string $str required
  119. * @param string $encoding default UTF-8
  120. * @return string
  121. */
  122. public static function lower($str, $encoding = null)
  123. {
  124. $encoding or $encoding = \Fuel::$encoding;
  125. return function_exists('mb_strtolower')
  126. ? mb_strtolower($str, $encoding)
  127. : strtolower($str);
  128. }
  129. /**
  130. * upper
  131. *
  132. * @param string $str required
  133. * @param string $encoding default UTF-8
  134. * @return string
  135. */
  136. public static function upper($str, $encoding = null)
  137. {
  138. $encoding or $encoding = \Fuel::$encoding;
  139. return function_exists('mb_strtoupper')
  140. ? mb_strtoupper($str, $encoding)
  141. : strtoupper($str);
  142. }
  143. /**
  144. * lcfirst
  145. *
  146. * Does not strtoupper first
  147. *
  148. * @param string $str required
  149. * @param string $encoding default UTF-8
  150. * @return string
  151. */
  152. public static function lcfirst($str, $encoding = null)
  153. {
  154. $encoding or $encoding = \Fuel::$encoding;
  155. return function_exists('mb_strtolower')
  156. ? mb_strtolower(mb_substr($str, 0, 1, $encoding), $encoding).
  157. mb_substr($str, 1, mb_strlen($str, $encoding), $encoding)
  158. : lcfirst($str);
  159. }
  160. /**
  161. * ucfirst
  162. *
  163. * Does not strtolower first
  164. *
  165. * @param string $str required
  166. * @param string $encoding default UTF-8
  167. * @return string
  168. */
  169. public static function ucfirst($str, $encoding = null)
  170. {
  171. $encoding or $encoding = \Fuel::$encoding;
  172. return function_exists('mb_strtoupper')
  173. ? mb_strtoupper(mb_substr($str, 0, 1, $encoding), $encoding).
  174. mb_substr($str, 1, mb_strlen($str, $encoding), $encoding)
  175. : ucfirst($str);
  176. }
  177. /**
  178. * ucwords
  179. *
  180. * First strtolower then ucwords
  181. *
  182. * ucwords normally doesn't strtolower first
  183. * but MB_CASE_TITLE does, so ucwords now too
  184. *
  185. * @param string $str required
  186. * @param string $encoding default UTF-8
  187. * @return string
  188. */
  189. public static function ucwords($str, $encoding = null)
  190. {
  191. $encoding or $encoding = \Fuel::$encoding;
  192. return function_exists('mb_convert_case')
  193. ? mb_convert_case($str, MB_CASE_TITLE, $encoding)
  194. : ucwords(strtolower($str));
  195. }
  196. /**
  197. * Creates a random string of characters
  198. *
  199. * @param string the type of string
  200. * @param int the number of characters
  201. * @return string the random string
  202. */
  203. public static function random($type = 'alnum', $length = 16)
  204. {
  205. switch($type)
  206. {
  207. case 'basic':
  208. return mt_rand();
  209. break;
  210. default:
  211. case 'alnum':
  212. case 'numeric':
  213. case 'nozero':
  214. case 'alpha':
  215. case 'distinct':
  216. case 'hexdec':
  217. switch ($type)
  218. {
  219. case 'alpha':
  220. $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  221. break;
  222. default:
  223. case 'alnum':
  224. $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  225. break;
  226. case 'numeric':
  227. $pool = '0123456789';
  228. break;
  229. case 'nozero':
  230. $pool = '123456789';
  231. break;
  232. case 'distinct':
  233. $pool = '2345679ACDEFHJKLMNPRSTUVWXYZ';
  234. break;
  235. case 'hexdec':
  236. $pool = '0123456789abcdef';
  237. break;
  238. }
  239. $str = '';
  240. for ($i=0; $i < $length; $i++)
  241. {
  242. $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
  243. }
  244. return $str;
  245. break;
  246. case 'unique':
  247. return md5(uniqid(mt_rand()));
  248. break;
  249. case 'sha1' :
  250. return sha1(uniqid(mt_rand(), true));
  251. break;
  252. }
  253. }
  254. /**
  255. * Returns a closure that will alternate between the args which to return.
  256. * If you call the closure with false as the arg it will return the value without
  257. * alternating the next time.
  258. *
  259. * @return Closure
  260. */
  261. public static function alternator()
  262. {
  263. // the args are the values to alternate
  264. $args = func_get_args();
  265. return function ($next = true) use ($args)
  266. {
  267. static $i = 0;
  268. return $args[($next ? $i++ : $i) % count($args)];
  269. };
  270. }
  271. /**
  272. * Parse the params from a string using strtr()
  273. *
  274. * @param string string to parse
  275. * @param array params to str_replace
  276. * @return string
  277. */
  278. public static function tr($string, $array = array())
  279. {
  280. if (is_string($string))
  281. {
  282. $tr_arr = array();
  283. foreach ($array as $from => $to)
  284. {
  285. $tr_arr[':'.$from] = $to;
  286. }
  287. unset($array);
  288. return strtr($string, $tr_arr);
  289. }
  290. else
  291. {
  292. return $string;
  293. }
  294. }
  295. }