Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 PHP生成随机字符串的二个例子

PHP生成随机字符串的二个例子

发布时间:2014-07-20   编辑:www.jquerycn.cn
PHP生成随机字符串的二个例子
例子1:
复制代码 代码如下:
< ?php
function genRandomString($len)
{
    $chars = array(
        "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", 
        "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", 
        "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", 
        "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", 
        "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", 
        "3", "4", "5", "6", "7", "8", "9"
    );
    $charsLen = count($chars) - 1;
    shuffle($chars);    // 将数组打乱
    $output = "";
    for ($i=0; $i<$len; $i++)
    {
        $output .= $chars[mt_rand(0, $charsLen)];
    } 
    return $output; 
}
$str = genRandomString(25);
$str .= "<br />";
$str .= genRandomString(25);
$str .= "<br />";
$str .= genRandomString(25);
echo $str;
?>
例子2:
复制代码 代码如下:
<?php
/* Generate Password
* Length : 8
*/
$str = "0123456789abcdefghijklmnopqrstuvwxyz";   //   输出字符集 
$n = 8;   //   输出串长度 
$len = strlen($str)-1;
for($j=0 ; $j<200 ; $j++){
for($i=0 ; $i<$n; $i++){
    $s .=  $str[rand(0,$len)]; 
}
echo $s . "<br/>";
$s = "";
}
?>

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

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