一个产生随机字符的函数(PHP版)

in 程序开发

function random($length, $numeric = 0) {
 mt_srand((double)microtime() * 1000000);
 if($numeric) {
  $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
 } else {
  $hash = '';
  $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
  $max = strlen($chars) - 1;
  for($i = 0; $i < $length; $i++) {
   $hash .= $chars[mt_rand(0, $max)];
  }
 }
 return $hash;
}

0 Comments

Leave a Reply

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>