str.php 2.8 KB

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