PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/system/RandomKey.php

https://github.com/monkeycraps/swoole_framework
PHP | 70 lines | 46 code | 0 blank | 24 comment | 3 complexity | 48be3444ad9d13cdf116566f01aba5f6 MD5 | raw file
  1. <?php
  2. class RandomKey
  3. {
  4. function getChineseCharacter()
  5. {
  6. $unidec = rand(19968, 24869);
  7. $unichr = '&#' . $unidec . ';';
  8. $zhcnchr = mb_convert_encoding($unichr, "UTF-8", "HTML-ENTITIES");
  9. return $zhcnchr;
  10. }
  11. /**
  12. * 随机生成一个字符串
  13. * @param $length
  14. * @param $number
  15. * @param $not_o0
  16. * @return unknown_type
  17. */
  18. static function string($length=8,$number=true,$not_o0=false)
  19. {
  20. $strings = 'ABCDEFGHIJKLOMNOPQRSTUVWXYZ'; //字符池
  21. $numbers = '0123456789'; //数字池
  22. if($not_o0)
  23. {
  24. $strings = str_replace('O','',$strings);
  25. $numbers = str_replace('0','',$numbers);
  26. }
  27. $pattern = $strings.$number;
  28. $max = strlen($pattern)-1;
  29. $key = '';
  30. for($i=0;$i<$length;$i++)
  31. {
  32. $key .= $pattern{mt_rand(0,$max)}; //生成php随机数
  33. }
  34. return $key;
  35. }
  36. /**
  37. * 按ID计算散列
  38. * @param $uid
  39. * @param $base
  40. * @return unknown_type
  41. */
  42. static function idhash($uid,$base=1000)
  43. {
  44. return intval($uid/$base);
  45. }
  46. /**
  47. * 按UNIX时间戳产生随机数
  48. * @param $length
  49. * @return unknown_type
  50. */
  51. static function randtime($rand_length=6)
  52. {
  53. list($usec, $sec) = explode(" ", microtime());
  54. $min = intval('1'.str_repeat('0',$rand_length-1));
  55. $max = intval(str_repeat('9',$rand_length));
  56. return substr($sec,-5).((int)$usec*100).rand($min,$max);
  57. }
  58. /**
  59. * 产生一个随机MD5字符的一部分
  60. * @param $length
  61. * @param $seed
  62. * @return unknown_type
  63. */
  64. static function randmd5($length=8,$seed=null)
  65. {
  66. if(empty($seed)) $seed = self::string(16);
  67. return substr(md5($seed.rand(111111,999999)),0,$length);
  68. }
  69. }
  70. ?>