Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 PHP随机字符串生成函数

PHP随机字符串生成函数

发布时间:2014-07-20   编辑:www.jquerycn.cn
PHP生成随机字符串的函数
PHP生成随机字符串的函数,可自定义生成的字符串长度。
复制代码 代码如下:
function random_string($length, $max=FALSE)
{
 if (is_int($max) && $max > $length)
  {
    $length = mt_rand($length, $max);
  }
  $output = '';
  
  for ($i=0; $i<$length; $i++)
  {
    $which = mt_rand(0,2);
    
    if ($which === 0)
    {
      $output .= mt_rand(0,9);
    }
    elseif ($which === 1)
    {
      $output .= chr(mt_rand(65,90));
    }
    else
    {
      $output .= chr(mt_rand(97,122));
    }
  }
  return $output;
}

您可能感兴趣的文章:
php生成随机产生六位数密码的代码
php生成指定位数(长度)的随机字符串
php随机生成4位数字验证码
php生成随机数的例子
用PHP生成随机数的函数
php生成随机字符串的函数
php生成N个不重复的随机数
PHP批量更新数据库的示例代码
PHP最强大的随机字符串生成函数
php生成随机数字和字母的实例代码

关键词: 随机字符串   
[关闭]