PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/str.php

https://github.com/joshuarubin/fuel_core
PHP | 256 lines | 136 code | 29 blank | 91 comment | 7 complexity | b6fb9e98bb8a66e7ce30cc38533a8160 MD5 | raw file
  1. <?php
  2. /**
  3. * Fuel is a fast, lightweight, community driven PHP5 framework.
  4. *
  5. * @package Fuel
  6. * @version 1.0
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2011 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. * Truncates a string to the given length. It will optionally preserve
  22. * HTML tags if $is_html is set to true.
  23. *
  24. * @param string $string the string to truncate
  25. * @param int $limit the number of characters to truncate too
  26. * @param string $continuation the string to use to denote it was truncated
  27. * @param bool $is_html whether the string has HTML
  28. * @return string the truncated string
  29. */
  30. public static function truncate($string, $limit, $continuation = '...', $is_html = false)
  31. {
  32. $offset = 0;
  33. $tags = array();
  34. if ($is_html)
  35. {
  36. preg_match_all('/<[^>]+>([^<]*)/', $string, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
  37. foreach ($matches as $match)
  38. {
  39. if($match[0][1] - $offset >= $limit)
  40. {
  41. break;
  42. }
  43. $tag = substr(strtok($match[0][0], " \t\n\r\0\x0B>"), 1);
  44. if($tag[0] != '/')
  45. {
  46. $tags[] = $tag;
  47. }
  48. elseif (end($tags) == substr($tag, 1))
  49. {
  50. array_pop($tags);
  51. }
  52. $offset += $match[1][1] - $match[0][1];
  53. }
  54. }
  55. $new_string = substr($string, 0, $limit = min(strlen($string), $limit + $offset));
  56. $new_string .= (strlen($string) > $limit ? $continuation : '');
  57. $new_string .= (count($tags = array_reverse($tags)) ? '</'.implode('></',$tags).'>' : '');
  58. return $new_string;
  59. }
  60. /**
  61. * Add's _1 to a string or increment the ending number to allow _2, _3, etc
  62. *
  63. * @param string $str required
  64. * @return string
  65. */
  66. public static function increment($str, $first = 1, $separator = '_')
  67. {
  68. preg_match('/(.+)'.$separator.'([0-9]+)$/', $str, $match);
  69. return isset($match[2]) ? $match[1].$separator.($match[2] + 1) : $str.$separator.$first;
  70. }
  71. /**
  72. * lower
  73. *
  74. * @param string $str required
  75. * @param string $encoding default UTF-8
  76. * @return string
  77. */
  78. public static function lower($str, $encoding = null)
  79. {
  80. $encoding or $encoding = \Fuel::$encoding;
  81. return function_exists('mb_strtolower')
  82. ? mb_strtolower($str, $encoding)
  83. : strtolower($str);
  84. }
  85. /**
  86. * upper
  87. *
  88. * @param string $str required
  89. * @param string $encoding default UTF-8
  90. * @return string
  91. */
  92. public static function upper($str, $encoding = null)
  93. {
  94. $encoding or $encoding = \Fuel::$encoding;
  95. return function_exists('mb_strtoupper')
  96. ? mb_strtoupper($str, $encoding)
  97. : strtoupper($str);
  98. }
  99. /**
  100. * lcfirst
  101. *
  102. * Does not strtoupper first
  103. *
  104. * @param string $str required
  105. * @param string $encoding default UTF-8
  106. * @return string
  107. */
  108. public static function lcfirst($str, $encoding = null)
  109. {
  110. $encoding or $encoding = \Fuel::$encoding;
  111. return function_exists('mb_strtolower')
  112. ? mb_strtolower(mb_substr($str, 0, 1, $encoding), $encoding).
  113. mb_substr($str, 1, mb_strlen($str, $encoding), $encoding)
  114. : ucfirst($str);
  115. }
  116. /**
  117. * ucfirst
  118. *
  119. * Does not strtolower first
  120. *
  121. * @param string $str required
  122. * @param string $encoding default UTF-8
  123. * @return string
  124. */
  125. public static function ucfirst($str, $encoding = null)
  126. {
  127. $encoding or $encoding = \Fuel::$encoding;
  128. return function_exists('mb_strtoupper')
  129. ? mb_strtoupper(mb_substr($str, 0, 1, $encoding), $encoding).
  130. mb_substr($str, 1, mb_strlen($str, $encoding), $encoding)
  131. : ucfirst($str);
  132. }
  133. /**
  134. * ucwords
  135. *
  136. * First strtolower then ucwords
  137. *
  138. * ucwords normally doesn't strtolower first
  139. * but MB_CASE_TITLE does, so ucwords now too
  140. *
  141. * @param string $str required
  142. * @param string $encoding default UTF-8
  143. * @return string
  144. */
  145. public static function ucwords($str, $encoding = null)
  146. {
  147. $encoding or $encoding = \Fuel::$encoding;
  148. return function_exists('mb_convert_case')
  149. ? mb_convert_case($str, MB_CASE_TITLE, $encoding)
  150. : ucwords(strtolower($str));
  151. }
  152. /**
  153. * Creates a random string of characters
  154. *
  155. * @param string the type of string
  156. * @param int the number of characters
  157. * @return string the random string
  158. */
  159. public static function random($type = 'alnum', $length = 16)
  160. {
  161. switch($type)
  162. {
  163. case 'basic':
  164. return mt_rand();
  165. break;
  166. default:
  167. case 'alnum':
  168. case 'numeric':
  169. case 'nozero':
  170. case 'alpha':
  171. case 'distinct':
  172. case 'hexdec':
  173. switch ($type)
  174. {
  175. case 'alpha':
  176. $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  177. break;
  178. default:
  179. case 'alnum':
  180. $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  181. break;
  182. case 'numeric':
  183. $pool = '0123456789';
  184. break;
  185. case 'nozero':
  186. $pool = '123456789';
  187. break;
  188. case 'distinct':
  189. $pool = '2345679ACDEFHJKLMNPRSTUVWXYZ';
  190. break;
  191. case 'hexdec':
  192. $pool = '0123456789abcdef';
  193. break;
  194. }
  195. $str = '';
  196. for ($i=0; $i < $length; $i++)
  197. {
  198. $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
  199. }
  200. return $str;
  201. break;
  202. case 'unique':
  203. return md5(uniqid(mt_rand()));
  204. break;
  205. case 'sha1' :
  206. return sha1(uniqid(mt_rand(), true));
  207. break;
  208. }
  209. }
  210. /**
  211. * Returns a closure that will alternate between the args which to return.
  212. * If you call the closure with false as the arg it will return the value without
  213. * alternating the next time.
  214. *
  215. * @return Closure
  216. */
  217. public static function alternator()
  218. {
  219. // the args are the values to alternate
  220. $args = func_get_args();
  221. return function ($next = true) use ($args)
  222. {
  223. static $i = 0;
  224. return $args[($next ? $i++ : $i) % count($args)];
  225. };
  226. }
  227. }