str.php 7.1 KB

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