str.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php namespace Laravel;
  2. class Str {
  3. /**
  4. * Convert a string to lowercase.
  5. *
  6. * <code>
  7. * // Convert a string to lowercase
  8. * echo Str::lower('STOP YELLING');
  9. * </code>
  10. *
  11. * @param string $value
  12. * @return string
  13. */
  14. public static function lower($value)
  15. {
  16. if (function_exists('mb_strtolower'))
  17. {
  18. return mb_strtolower($value, Config::$items['application']['encoding']);
  19. }
  20. return strtolower($value);
  21. }
  22. /**
  23. * Convert a string to uppercase.
  24. *
  25. * <code>
  26. * // Convert a string to uppercase
  27. * echo Str::upper('speak louder');
  28. * </code>
  29. *
  30. * @param string $value
  31. * @return string
  32. */
  33. public static function upper($value)
  34. {
  35. if (function_exists('mb_strtoupper'))
  36. {
  37. return mb_strtoupper($value, Config::$items['application']['encoding']);
  38. }
  39. return strtoupper($value);
  40. }
  41. /**
  42. * Convert a string to title case (ucwords equivalent).
  43. *
  44. * <code>
  45. * // Convert a string to title case
  46. * echo Str::title('taylor otwell');
  47. * </code>
  48. *
  49. * @param string $value
  50. * @return string
  51. */
  52. public static function title($value)
  53. {
  54. if (function_exists('mb_convert_case'))
  55. {
  56. return mb_convert_case($value, MB_CASE_TITLE, Config::$items['application']['encoding']);
  57. }
  58. return ucwords(strtolower($value));
  59. }
  60. /**
  61. * Get the length of a string.
  62. *
  63. * <code>
  64. * // Get the length of a string
  65. * echo Str::length('taylor otwell');
  66. * </code>
  67. *
  68. * @param string $value
  69. * @return int
  70. */
  71. public static function length($value)
  72. {
  73. if (function_exists('mb_strlen'))
  74. {
  75. return mb_strlen($value, Config::$items['application']['encoding']);
  76. }
  77. return strlen($value);
  78. }
  79. /**
  80. * Limit the number of characters in a string.
  81. *
  82. * <code>
  83. * // Returns "Tay..."
  84. * echo Str::limit('Taylor Otwell', 3);
  85. *
  86. * // Limit the number of characters and append a custom ending
  87. * echo Str::limit('Taylor Otwell', 3, '---');
  88. * </code>
  89. *
  90. * @param string $value
  91. * @param int $limit
  92. * @param string $end
  93. * @return string
  94. */
  95. public static function limit($value, $limit = 100, $end = '...')
  96. {
  97. if (static::length($value) <= $limit) return $value;
  98. if (function_exists('mb_substr'))
  99. {
  100. return mb_substr($value, 0, $limit, Config::$items['application']['encoding']).$end;
  101. }
  102. return substr($value, 0, $limit).$end;
  103. }
  104. /**
  105. * Limit the number of words in a string
  106. *
  107. * <code>
  108. * // Returns "This is a..."
  109. * echo Str::words('This is a sentence.', 3);
  110. *
  111. * // Limit the number of words and append a custom ending
  112. * echo Str::words('This is a sentence.', 3, '---');
  113. * </code>
  114. *
  115. * @param string $value
  116. * @param int $length
  117. * @param string $end
  118. * @return string
  119. */
  120. public static function words($value, $words = 100, $end = '...')
  121. {
  122. $count = str_word_count($value, 1);
  123. if ($count <= $words) return $value;
  124. return implode(' ', array_slice($count, 0, $words)).$end;
  125. }
  126. /**
  127. * Convert a string to 7-bit ASCII.
  128. *
  129. * <code>
  130. * // Returns "Deuxieme Article"
  131. * echo Str::ascii('Deuxième Article');
  132. * </code>
  133. *
  134. * @param string $value
  135. * @return string
  136. */
  137. public static function ascii($value)
  138. {
  139. $foreign = Config::get('ascii');
  140. $value = preg_replace(array_keys($foreign), array_values($foreign), $value);
  141. return preg_replace('/[^\x09\x0A\x0D\x20-\x7E]/', '', $value);
  142. }
  143. /**
  144. * Generate a random alpha or alpha-numeric string.
  145. *
  146. * <code>
  147. * // Generate a 40 character random, alpha-numeric string
  148. * echo Str::random(40);
  149. *
  150. * // Generate a 16 character random, alphabetic string
  151. * echo Str::random(16, 'alpha');
  152. * <code>
  153. *
  154. * @param int $length
  155. * @param string $type
  156. * @return string
  157. */
  158. public static function random($length, $type = 'alnum')
  159. {
  160. return substr(str_shuffle(str_repeat(static::pool($type), 5)), 0, $length);
  161. }
  162. /**
  163. * Get the character pool for a given type of random string.
  164. *
  165. * @param string $type
  166. * @return string
  167. */
  168. protected static function pool($type)
  169. {
  170. switch ($type)
  171. {
  172. case 'alpha':
  173. return 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  174. case 'alnum':
  175. return '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  176. default:
  177. throw new \DomainException("Invalid random string type [$type].");
  178. }
  179. }
  180. }