分享

PHP生成随机字符串的函数

 乡间小路有风景 2013-04-13
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;
}
?>
本文出处参考:http://www./article/445.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多