<?php
//通过类 写随机字符串
class RandomString {
//属性
private $length;
private $type;
//方法
public function __construct($type=1,$length=4){
$this->type = $type;
$this->length = $length;
}
public function getRandomString(){
//1 数字
//2 字母
//3 数字+字母
switch ($this->type){
case 1:
return join(array_rand(range(0,9),$this->length));
break;
case 2:
return join(array_rand(array_flip(array_merge(range('a','z'),range('A','Z'))),$this->length));
break;
case 3:
return join(array_rand(array_flip(array_merge(range(0,9),range('a','z'),range('A','Z'))),$this->length));
break;
}
}
}
$r = new RandomString(3,4);
echo $r->getRandomString();
PHP随机生成字符串
最新推荐文章于 2023-03-15 16:29:11 发布