str.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php namespace System;
  2. class Str {
  3. /**
  4. * Convert HTML characters to entities.
  5. *
  6. * @param string $value
  7. * @return string
  8. */
  9. public static function entities($value)
  10. {
  11. return htmlentities($value, ENT_QUOTES, Config::get('application.encoding'), false);
  12. }
  13. /**
  14. * Convert a string to lowercase.
  15. *
  16. * @param string $value
  17. * @return string
  18. */
  19. public static function lower($value)
  20. {
  21. return function_exists('mb_strtolower') ? mb_strtolower($value, Config::get('application.encoding')) : strtolower($value);
  22. }
  23. /**
  24. * Convert a string to uppercase.
  25. *
  26. * @param string $value
  27. * @return string
  28. */
  29. public static function upper($value)
  30. {
  31. return function_exists('mb_strtoupper') ? mb_strtoupper($value, Config::get('application.encoding')) : strtoupper($value);
  32. }
  33. /**
  34. * Convert a string to title case (ucwords).
  35. *
  36. * @param string $value
  37. * @return string
  38. */
  39. public static function title($value)
  40. {
  41. return (function_exists('mb_convert_case')) ? mb_convert_case($value, MB_CASE_TITLE, Config::get('application.encoding')) : ucwords(strtolower($value));
  42. }
  43. /**
  44. * Generate a random alpha-numeric string.
  45. *
  46. * @param int $length
  47. * @return string
  48. */
  49. public static function random($length = 16)
  50. {
  51. $pool = str_split('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 1);
  52. $value = '';
  53. for ($i = 0; $i < $length; $i++)
  54. {
  55. $value .= $pool[mt_rand(0, 61)];
  56. }
  57. return $value;
  58. }
  59. }