RandomUtil.php 603 B

123456789101112131415161718192021222324252627
  1. <?php
  2. namespace common\components\payment\joinpay\util;
  3. /**
  4. * 随机数生成工具类
  5. * Class RandomUtil
  6. * @package utils
  7. */
  8. class RandomUtil {
  9. const CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  10. /**
  11. * 生成指定长度的随机字符串
  12. * @entity int $length
  13. * @return string
  14. */
  15. public static function randomStr( $length = 16 ) {
  16. $str = "";
  17. $strLen = strlen(static::CHARS) - 1;
  18. for ($i = 0; $i < $length; $i++) {
  19. $str .= static::CHARS[mt_rand(0, $strLen)];
  20. }
  21. return $str;
  22. }
  23. }