str.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php namespace Laravel;
  2. class Str {
  3. /**
  4. * Convert a string to lowercase.
  5. *
  6. * @param string $value
  7. * @return string
  8. */
  9. public static function lower($value)
  10. {
  11. return function_exists('mb_strtolower') ? mb_strtolower($value, static::encoding()) : strtolower($value);
  12. }
  13. /**
  14. * Convert a string to uppercase.
  15. *
  16. * @param string $value
  17. * @return string
  18. */
  19. public static function upper($value)
  20. {
  21. return function_exists('mb_strtoupper') ? mb_strtoupper($value, static::encoding()) : strtoupper($value);
  22. }
  23. /**
  24. * Convert a string to title case (ucwords).
  25. *
  26. * @param string $value
  27. * @return string
  28. */
  29. public static function title($value)
  30. {
  31. return (function_exists('mb_convert_case')) ? mb_convert_case($value, MB_CASE_TITLE, static::encoding()) : ucwords(strtolower($value));
  32. }
  33. /**
  34. * Get the length of a string.
  35. *
  36. * @param string $value
  37. * @return int
  38. */
  39. public static function length($value)
  40. {
  41. return function_exists('mb_strlen') ? mb_strlen($value, static::encoding()) : strlen($value);
  42. }
  43. /**
  44. * Convert a string to 7-bit ASCII.
  45. *
  46. * @param string $value
  47. * @return string
  48. */
  49. public static function ascii($value)
  50. {
  51. $foreign = IoC::container()->resolve('laravel.config')->get('ascii');
  52. $value = preg_replace(array_keys($foreign), array_values($foreign), $value);
  53. return preg_replace('/[^\x09\x0A\x0D\x20-\x7E]/', '', $value);
  54. }
  55. /**
  56. * Generate a random alpha or alpha-numeric string.
  57. *
  58. * Supported types: 'alpha_num' and 'alpha'.
  59. *
  60. * @param int $length
  61. * @param string $type
  62. * @return string
  63. */
  64. public static function random($length = 16, $type = 'alpha_num')
  65. {
  66. $value = '';
  67. $pool_length = strlen($pool = static::pool($type)) - 1;
  68. for ($i = 0; $i < $length; $i++)
  69. {
  70. $value .= $pool[mt_rand(0, $pool_length)];
  71. }
  72. return $value;
  73. }
  74. /**
  75. * Get a chracter pool.
  76. *
  77. * @param string $type
  78. * @return string
  79. */
  80. private static function pool($type = 'alpha_num')
  81. {
  82. switch ($type)
  83. {
  84. case 'alpha_num':
  85. return '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  86. default:
  87. return 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  88. }
  89. }
  90. /**
  91. * Get the application encoding from the configuration class.
  92. *
  93. * @return string
  94. */
  95. public static function encoding()
  96. {
  97. return IoC::container()->resolve('laravel.config')->get('application.encoding');
  98. }
  99. }