str.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/', $value, $matches);
  140. if (static::length($value) == static::length($matches[0]))
  141. {
  142. $end = '';
  143. }
  144. return rtrim($matches[0]).$end;
  145. }
  146. /**
  147. * Get the singular form of the given word.
  148. *
  149. * @param string $value
  150. * @return string
  151. */
  152. public static function singular($value)
  153. {
  154. return static::pluralizer()->singular($value);
  155. }
  156. /**
  157. * Get the plural form of the given word.
  158. *
  159. * <code>
  160. * // Returns the plural form of "child"
  161. * $plural = Str::plural('child', 10);
  162. *
  163. * // Returns the singular form of "octocat" since count is one
  164. * $plural = Str::plural('octocat', 1);
  165. * </code>
  166. *
  167. * @param string $value
  168. * @param int $count
  169. * @return string
  170. */
  171. public static function plural($value, $count = 2)
  172. {
  173. return static::pluralizer()->plural($value, $count);
  174. }
  175. /**
  176. * Get the pluralizer instance.
  177. *
  178. * @return Pluralizer
  179. */
  180. protected static function pluralizer()
  181. {
  182. $config = Config::get('strings');
  183. return static::$pluralizer ?: static::$pluralizer = new Pluralizer($config);
  184. }
  185. /**
  186. * Generate a URL friendly "slug" from a given string.
  187. *
  188. * <code>
  189. * // Returns "this-is-my-blog-post"
  190. * $slug = Str::slug('This is my blog post!');
  191. *
  192. * // Returns "this_is_my_blog_post"
  193. * $slug = Str::slug('This is my blog post!', '_');
  194. * </code>
  195. *
  196. * @param string $title
  197. * @param string $separator
  198. * @return string
  199. */
  200. public static function slug($title, $separator = '-')
  201. {
  202. $title = static::ascii($title);
  203. // Remove all characters that are not the separator, letters, numbers, or whitespace.
  204. $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', static::lower($title));
  205. // Replace all separator characters and whitespace by a single separator
  206. $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
  207. return trim($title, $separator);
  208. }
  209. /**
  210. * Convert a string to 7-bit ASCII.
  211. *
  212. * This is helpful for converting UTF-8 strings for usage in URLs, etc.
  213. *
  214. * @param string $value
  215. * @return string
  216. */
  217. public static function ascii($value)
  218. {
  219. $foreign = Config::get('strings.ascii');
  220. $value = preg_replace(array_keys($foreign), array_values($foreign), $value);
  221. return preg_replace('/[^\x09\x0A\x0D\x20-\x7E]/', '', $value);
  222. }
  223. /**
  224. * Convert a string to an underscored, camel-cased class name.
  225. *
  226. * This method is primarily used to format task and controller names.
  227. *
  228. * <code>
  229. * // Returns "Task_Name"
  230. * $class = Str::classify('task_name');
  231. *
  232. * // Returns "Taylor_Otwell"
  233. * $class = Str::classify('taylor otwell')
  234. * </code>
  235. *
  236. * @param string $value
  237. * @return string
  238. */
  239. public static function classify($value)
  240. {
  241. $search = array('_', '-', '.');
  242. return str_replace(' ', '_', static::title(str_replace($search, ' ', $value)));
  243. }
  244. /**
  245. * Return the "URI" style segments in a given string.
  246. *
  247. * @param string $value
  248. * @return array
  249. */
  250. public static function segments($value)
  251. {
  252. return array_diff(explode('/', trim($value, '/')), array(''));
  253. }
  254. /**
  255. * Generate a random alpha or alpha-numeric string.
  256. *
  257. * <code>
  258. * // Generate a 40 character random alpha-numeric string
  259. * echo Str::random(40);
  260. *
  261. * // Generate a 16 character random alphabetic string
  262. * echo Str::random(16, 'alpha');
  263. * <code>
  264. *
  265. * @param int $length
  266. * @param string $type
  267. * @return string
  268. */
  269. public static function random($length, $type = 'alnum')
  270. {
  271. return substr(str_shuffle(str_repeat(static::pool($type), 5)), 0, $length);
  272. }
  273. /**
  274. * Determine if a given string matches a given pattern.
  275. *
  276. * @param string $pattern
  277. * @param string $value
  278. * @return bool
  279. */
  280. public static function is($pattern, $value)
  281. {
  282. // Asterisks are translated into zero-or-more regular expression wildcards
  283. // to make it convenient to check if the URI starts with a given pattern
  284. // such as "library/*". This is only done when not root.
  285. if ($pattern !== '/')
  286. {
  287. $pattern = str_replace('*', '(.*)', $pattern).'\z';
  288. }
  289. else
  290. {
  291. $pattern = '^/$';
  292. }
  293. return preg_match('#'.$pattern.'#', $value);
  294. }
  295. /**
  296. * Get the character pool for a given type of random string.
  297. *
  298. * @param string $type
  299. * @return string
  300. */
  301. protected static function pool($type)
  302. {
  303. switch ($type)
  304. {
  305. case 'alpha':
  306. return 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  307. case 'alnum':
  308. return '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  309. default:
  310. throw new \Exception("Invalid random string type [$type].");
  311. }
  312. }
  313. }