找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索本站精品资源

首页 教程频道 查看内容

php如何生成不重复随机字符串

作者:模板之家 2020-9-26 20:17 4271人关注

php生成不重复随机字符串的方法:1、使用时间戳作为原始字符串,再随机生成五个字符随机插入任意位置,生成新的字符串,代码为【$position=rand()%strlen($chars)】;2、使用【array_slice】函数随机抽取。

php生成不重复随机字符串的方法:1、使用时间戳作为原始字符串,再随机生成五个字符随机插入任意位置,生成新的字符串,代码为【$position=rand()%strlen($chars)】;2、使用【array_slice】函数随机抽取。

php生成不重复随机字符串的方法:

1、使用时间戳作为原始字符串,再随机生成五个字符随机插入任意位置,生成新的字符串,保证不重复

function rand($len)
    {
        $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
        $string=time();
        for(;$len>=1;$len--)
        {
            $position=rand()%strlen($chars);
            $position2=rand()%strlen($string);
            $string=substr_replace($string,substr($chars,$position,1),$position2,0);
        }
        return $string;
    }

2、使用array_slice()函数随机抽取

<?php
$numbers = range (1,50);
//shuffle 将数组顺序随即打乱
shuffle ($numbers);
//array_slice 取该数组中的某一段
$num=6;
$result = array_slice($numbers,0,$num);
print_r($result);
?>

相关学习推荐:php编程(视频)

以上就是php如何生成不重复随机字符串的详细内容,更多请关注模板之家(www.mb5.com.cn)其它相关文章!


路过

雷人

握手

鲜花

鸡蛋
来自: 网络收集

全部回复(0)