yii2 随机字符或随机密码有一下几种,记录一下
第一种、自带
use Faker\Provider\Uuid;
echo Uuid::uuid();
效果:cb1bf443-d827-3f9c-bd26-f8b457f810c2
第二种、随机大小写字符
函数方法
function rand_zm($length){
$str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';//62个字符
$strlen = 62;
while($length > $strlen){
$str .= $str;
$strlen += 62;
}
$str = str_shuffle($str);
return substr($str,0,$length);
}
echo rand_zm(16);//16位大小写随机码
效果:2sIpGxNA3uOwiEfC