str.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. *
  10. * // Convert a UTF-8 string to lowercase
  11. * echo Str::lower('Τάχιστη');
  12. * </code>
  13. *
  14. * @param string $value
  15. * @return string
  16. */
  17. public static function lower($value)
  18. {
  19. if (function_exists('mb_strtolower'))
  20. {
  21. return mb_strtolower($value, Config::get('application.encoding'));
  22. }
  23. return strtolower($value);
  24. }
  25. /**
  26. * Convert a string to uppercase.
  27. *
  28. * <code>
  29. * // Convert a string to uppercase
  30. * echo Str::upper('speak louder');
  31. *
  32. * // Convert a UTF-8 string to uppercase
  33. * echo Str::upper('Τάχιστη');
  34. * </code>
  35. *
  36. * @param string $value
  37. * @return string
  38. */
  39. public static function upper($value)
  40. {
  41. if (function_exists('mb_strtoupper'))
  42. {
  43. return mb_strtoupper($value, Config::get('application.encoding'));
  44. }
  45. return strtoupper($value);
  46. }
  47. /**
  48. * Convert a string to title case (ucwords equivalent).
  49. *
  50. * <code>
  51. * // Convert a string to title case
  52. * echo Str::title('taylor otwell');
  53. *
  54. * // Convert a UTF-8 string to title case
  55. * echo Str::title('Τάχιστη αλώπηξ');
  56. * </code>
  57. *
  58. * @param string $value
  59. * @return string
  60. */
  61. public static function title($value)
  62. {
  63. if (function_exists('mb_convert_case'))
  64. {
  65. return mb_convert_case($value, MB_CASE_TITLE, Config::get('application.encoding'));
  66. }
  67. return ucwords(strtolower($value));
  68. }
  69. /**
  70. * Get the length of a string.
  71. *
  72. * <code>
  73. * // Get the length of a string
  74. * echo Str::length('taylor otwell');
  75. *
  76. * // Get the length of a UTF-8 string
  77. * echo Str::length('Τάχιστη αλώπηξ');
  78. * </code>
  79. *
  80. * @param string $value
  81. * @return int
  82. */
  83. public static function length($value)
  84. {
  85. if (function_exists('mb_strlen'))
  86. {
  87. return mb_strlen($value, Config::get('application.encoding'));
  88. }
  89. return strlen($value);
  90. }
  91. /**
  92. * Convert a string to 7-bit ASCII.
  93. *
  94. * <code>
  95. * // Returns "Deuxieme Article"
  96. * echo Str::ascii('Deuxième Article');
  97. * </code>
  98. *
  99. * @param string $value
  100. * @return string
  101. */
  102. public static function ascii($value)
  103. {
  104. $foreign = Config::get('ascii');
  105. $value = preg_replace(array_keys($foreign), array_values($foreign), $value);
  106. return preg_replace('/[^\x09\x0A\x0D\x20-\x7E]/', '', $value);
  107. }
  108. /**
  109. * Generate a random alpha or alpha-numeric string.
  110. *
  111. * <code>
  112. * // Generate a 40 character random, alpha-numeric string
  113. * echo Str::random(40);
  114. *
  115. * // Generate a 16 character random, alphabetic string
  116. * echo Str::random(16, 'alpha');
  117. * <code>
  118. *
  119. * @param int $length
  120. * @param string $type
  121. * @return string
  122. */
  123. public static function random($length, $type = 'alnum')
  124. {
  125. $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  126. return substr(str_shuffle(str_repeat(($type == 'alnum') ? $pool.'0123456789' : $pool, 5)), 0, $length);
  127. }
  128. }