str.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php namespace Laravel;
  2. class Str {
  3. /**
  4. * The pluralizer instance.
  5. *
  6. * @var Pluralizer
  7. */
  8. public static $pluralizer;
  9. /**
  10. * Get the default string encoding for the application.
  11. *
  12. * This method is simply a short-cut to Config::get('application.encoding').
  13. *
  14. * @return string
  15. */
  16. public static function encoding()
  17. {
  18. return Config::get('application.encoding');
  19. }
  20. /**
  21. * Get the length of a string.
  22. *
  23. * <code>
  24. * // Get the length of a string
  25. * $length = Str::length('Taylor Otwell');
  26. *
  27. * // Get the length of a multi-byte string
  28. * $length = Str::length('Τάχιστη')
  29. * </code>
  30. *
  31. * @param string $value
  32. * @return int
  33. */
  34. public static function length($value)
  35. {
  36. return (MB_STRING) ? mb_strlen($value, static::encoding()) : strlen($value);
  37. }
  38. /**
  39. * Convert a string to lowercase.
  40. *
  41. * <code>
  42. * // Convert a string to lowercase
  43. * $lower = Str::lower('Taylor Otwell');
  44. *
  45. * // Convert a multi-byte string to lowercase
  46. * $lower = Str::lower('Τάχιστη');
  47. * </code>
  48. *
  49. * @param string $value
  50. * @return string
  51. */
  52. public static function lower($value)
  53. {
  54. return (MB_STRING) ? mb_strtolower($value, static::encoding()) : strtolower($value);
  55. }
  56. /**
  57. * Convert a string to uppercase.
  58. *
  59. * <code>
  60. * // Convert a string to uppercase
  61. * $upper = Str::upper('Taylor Otwell');
  62. *
  63. * // Convert a multi-byte string to uppercase
  64. * $upper = Str::upper('Τάχιστη');
  65. * </code>
  66. *
  67. * @param string $value
  68. * @return string
  69. */
  70. public static function upper($value)
  71. {
  72. return (MB_STRING) ? mb_strtoupper($value, static::encoding()) : strtoupper($value);
  73. }
  74. /**
  75. * Convert a string to title case (ucwords equivalent).
  76. *
  77. * <code>
  78. * // Convert a string to title case
  79. * $title = Str::title('taylor otwell');
  80. *
  81. * // Convert a multi-byte string to title case
  82. * $title = Str::title('νωθρού κυνός');
  83. * </code>
  84. *
  85. * @param string $value
  86. * @return string
  87. */
  88. public static function title($value)
  89. {
  90. if (MB_STRING)
  91. {
  92. return mb_convert_case($value, MB_CASE_TITLE, static::encoding());
  93. }
  94. return ucwords(strtolower($value));
  95. }
  96. /**
  97. * Limit the number of characters in a string.
  98. *
  99. * <code>
  100. * // Returns "Tay..."
  101. * echo Str::limit('Taylor Otwell', 3);
  102. *
  103. * // Limit the number of characters and append a custom ending
  104. * echo Str::limit('Taylor Otwell', 3, '---');
  105. * </code>
  106. *
  107. * @param string $value
  108. * @param int $limit
  109. * @param string $end
  110. * @return string
  111. */
  112. public static function limit($value, $limit = 100, $end = '...')
  113. {
  114. if (static::length($value) <= $limit) return $value;
  115. if (MB_STRING)
  116. {
  117. return mb_substr($value, 0, $limit, static::encoding()).$end;
  118. }
  119. return substr($value, 0, $limit).$end;
  120. }
  121. /**
  122. * Limit the number of words in a string.
  123. *
  124. * <code>
  125. * // Returns "This is a..."
  126. * echo Str::words('This is a sentence.', 3);
  127. *
  128. * // Limit the number of words and append a custom ending
  129. * echo Str::words('This is a sentence.', 3, '---');
  130. * </code>
  131. *
  132. * @param string $value
  133. * @param int $words
  134. * @param string $end
  135. * @return string
  136. */
  137. public static function words($value, $words = 100, $end = '...')
  138. {
  139. if (trim($value) == '') return '';
  140. preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/u', $value, $matches);
  141. if (static::length($value) == static::length($matches[0]))
  142. {
  143. $end = '';
  144. }
  145. return rtrim($matches[0]).$end;
  146. }
  147. /**
  148. * Get the singular form of the given word.
  149. *
  150. * @param string $value
  151. * @return string
  152. */
  153. public static function singular($value)
  154. {
  155. return static::pluralizer()->singular($value);
  156. }
  157. /**
  158. * Get the plural form of the given word.
  159. *
  160. * <code>
  161. * // Returns the plural form of "child"
  162. * $plural = Str::plural('child', 10);
  163. *
  164. * // Returns the singular form of "octocat" since count is one
  165. * $plural = Str::plural('octocat', 1);
  166. * </code>
  167. *
  168. * @param string $value
  169. * @param int $count
  170. * @return string
  171. */
  172. public static function plural($value, $count = 2)
  173. {
  174. return static::pluralizer()->plural($value, $count);
  175. }
  176. /**
  177. * Get the pluralizer instance.
  178. *
  179. * @return Pluralizer
  180. */
  181. protected static function pluralizer()
  182. {
  183. $config = Config::get('strings');
  184. return static::$pluralizer ?: static::$pluralizer = new Pluralizer($config);
  185. }
  186. /**
  187. * Generate a URL friendly "slug" from a given string.
  188. *
  189. * <code>
  190. * // Returns "this-is-my-blog-post"
  191. * $slug = Str::slug('This is my blog post!');
  192. *
  193. * // Returns "this_is_my_blog_post"
  194. * $slug = Str::slug('This is my blog post!', '_');
  195. * </code>
  196. *
  197. * @param string $title
  198. * @param string $separator
  199. * @return string
  200. */
  201. public static function slug($title, $separator = '-')
  202. {
  203. $title = static::ascii($title);
  204. // Remove all characters that are not the separator, letters, numbers, or whitespace.
  205. $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', static::lower($title));
  206. // Replace all separator characters and whitespace by a single separator
  207. $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
  208. return trim($title, $separator);
  209. }
  210. /**
  211. * Convert a string to 7-bit ASCII.
  212. *
  213. * This is helpful for converting UTF-8 strings for usage in URLs, etc.
  214. *
  215. * @param string $value
  216. * @return string
  217. */
  218. public static function ascii($value)
  219. {
  220. $foreign = Config::get('strings.ascii');
  221. $value = preg_replace(array_keys($foreign), array_values($foreign), $value);
  222. return preg_replace('/[^\x09\x0A\x0D\x20-\x7E]/', '', $value);
  223. }
  224. /**
  225. * Convert a string to an underscored, camel-cased class name.
  226. *
  227. * This method is primarily used to format task and controller names.
  228. *
  229. * <code>
  230. * // Returns "Task_Name"
  231. * $class = Str::classify('task_name');
  232. *
  233. * // Returns "Taylor_Otwell"
  234. * $class = Str::classify('taylor otwell')
  235. * </code>
  236. *
  237. * @param string $value
  238. * @return string
  239. */
  240. public static function classify($value)
  241. {
  242. $search = array('_', '-', '.');
  243. return str_replace(' ', '_', static::title(str_replace($search, ' ', $value)));
  244. }
  245. /**
  246. * Return the "URI" style segments in a given string.
  247. *
  248. * @param string $value
  249. * @return array
  250. */
  251. public static function segments($value)
  252. {
  253. return array_diff(explode('/', trim($value, '/')), array(''));
  254. }
  255. /**
  256. * Generate a random alpha or alpha-numeric string.
  257. *
  258. * <code>
  259. * // Generate a 40 character random alpha-numeric string
  260. * echo Str::random(40);
  261. *
  262. * // Generate a 16 character random alphabetic string
  263. * echo Str::random(16, 'alpha');
  264. * <code>
  265. *
  266. * @param int $length
  267. * @param string $type
  268. * @return string
  269. */
  270. public static function random($length, $type = 'alnum')
  271. {
  272. return substr(str_shuffle(str_repeat(static::pool($type), 5)), 0, $length);
  273. }
  274. /**
  275. * Determine if a given string matches a given pattern.
  276. *
  277. * @param string $pattern
  278. * @param string $value
  279. * @return bool
  280. */
  281. public static function is($pattern, $value)
  282. {
  283. // Asterisks are translated into zero-or-more regular expression wildcards
  284. // to make it convenient to check if the URI starts with a given pattern
  285. // such as "library/*". This is only done when not root.
  286. if ($pattern !== '/')
  287. {
  288. $pattern = str_replace('*', '(.*)', $pattern).'\z';
  289. }
  290. else
  291. {
  292. $pattern = '^/$';
  293. }
  294. return preg_match('#'.$pattern.'#', $value);
  295. }
  296. /**
  297. * Get the character pool for a given type of random string.
  298. *
  299. * @param string $type
  300. * @return string
  301. */
  302. protected static function pool($type)
  303. {
  304. switch ($type)
  305. {
  306. case 'alpha':
  307. return 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  308. case 'alnum':
  309. return '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  310. default:
  311. throw new \Exception("Invalid random string type [$type].");
  312. }
  313. }
  314. }