根据用户指定的格式和规则生成随机字符串,支持多层嵌套,支持常量转换。附件中包含使用实例和默认的规则文件。
randex.phpformat = is_null($format) ? : $format;
if(is_null($rules))
//如果不指定自定义规则,则读取目录下的rulesdefault.ini作为规则
$rules = file_get_contents(__dir__./rulesdefault.ini);
$this->config($rules, $const);
}
//设置格式($type == 0)、规则(1)、常量(2)
public function set($str = null, $type) {
switch($type) {
case 0:
$this->format = is_null($str) ? : $str;
break;
case 1:
if(is_null($str))
$str = file_get_contents(rulesdefault.ini);
$this->config($str);
break;
case 2:
$this->config(null, $str);
break;
}
}
//根据已配置的规则生成随机字符串,number为生成的数量(默认为1)。若不为1,返回二维数组。
public function generate($number = 1) {
if($number return $this->format;
for($i = 0; $i $str = $this->format;
//每次替换字符串后进行一次判断,若字符串内容无更改,则结束循环
do {
$tempstr = $str;
$str = $this->formatconvert($str);
$str = $this->constconvert($str);
} while($tempstr !== $str);
if($number == 1)
return $str;
$ret[] = $str;
}
return $ret;
}
//析构函数
public function __destruct() {}
//解析规则,以多维数组形式保存在私有成员变量中
private function config($rules = null, $const = null) {
//使用换行符分割
if(!is_null($rules)) {
unset($this->rules);
$rulesarr = explode(\r\n, $rules);
$arrcount = count($rulesarr);
if($arrcount == 0)
$this->rules = array(array(), array(array()));
for($i = 0; $i $temparr = explode(::, $rulesarr[$i], 2);
if(count($temparr) != 2)
continue;
$this->rules[0][] = $temparr[0];
$this->rules[1][] = explode(,, $temparr[1]);
}
}
if(!is_null($const)) {
unset($this->const);
$constarr = explode(\r\n, $const);
$arrcount = count($constarr);
if($arrcount == 0)
$this->const = array(array(), array());
for($i = 0; $i $temparr = explode(::, $constarr[$i], 2);
if(count($temparr) != 2)
continue;
$this->const[0][] = $temparr[0];
$this->const[1][] = $temparr[1];
}
}
}
//使用指定规则规则进行字符串转换
private function formatconvert($str) {
$c = count($this->rules[0]);
$str = self::innerconvert($str);
for($i = 0; $i $find = [*.$this->rules[0][$i].:;
while(strpos($str, $find) !== false) {
$replacecount = count($this->rules[1][$i]);
$rangestr = strbetween($str, $find, *]);
$rangearr = explode(,, $rangestr, 2);
if(count($rangearr) != 2)
break;
mt_srand();
//生成的次数
$loop = mt_rand($rangearr[0], $rangearr[1]);
$tempstr = ;
for($j = 0; $j $tempstr .= $this->rules[1][$i][mt_rand(0, $replacecount - 1)];
$str = str_replace_once($find.$rangestr.*], $tempstr, $str);
}
}
return $str;
}
//转换常量
private function constconvert($str) {
//转换为逗号
$str = str_replace({*c*}, ,, $str);
//转换为换行符,可根据情况改为\n、\r、\r\n
$str = str_replace({*b*},
, $str);
$c = count($this->const[0]);
for($i = 0; $i $find = {*.$this->const[0][$i].*};
$str = str_replace($find, $this->const[1][$i], $str);
}
return $str;
}
//使用内置的规则进行字符串转换
private static function innerconvert($str) {
mt_srand();
//随机英文字母
while(strpos($str, [*a:) !== false) {
$result = strbetween($str, [*a:, *]);
$arr = explode(,, $result, 3);
if(count($arr) break;
if(count($arr) == 2)
$arr[] = 0;
$replace = randalphabet(mt_rand((int)$arr[0], (int)$arr[1]), (int)$arr[2]);
$str = str_replace_once([*a:.$result.*], $replace, $str);
}
//随机汉字、数字、字母+数字
for($i = 0; $i $prefix = array([*c:, [*n:, [*s:, [*i:);
while(strpos($str, $prefix[$i]) !== false) {
$result = strbetween($str, $prefix[$i], *]);
$arr = explode(,, $result, 2);
if(count($arr) != 2)
break;
if($i == 3)
$replace = randinteger((int)$arr[0], (int)$arr[1]);
else
$replace = randall($i, mt_rand((int)$arr[0], (int)$arr[1]));
$str = str_replace_once($prefix[$i].$result.*], $replace, $str);
}
}
//获取系统时间
while(strpos($str, [*d:) !== false) {
$result = strbetween($str, [*d:, *]);
$replace = date($result);
$str = str_replace([*d:.$result.*], $replace, $str);
}
return $str;
}
private $format;
private $rules;
private $const;
}functions.php 0; $number--)
//取随机gbk中文
$randstr .= chr(mt_rand(176, 218)).chr(mt_rand(176, 218));
//转换为utf-8
$randstr = iconv(gb2312, utf-8, $randstr);
return $randstr;
}
//生成指定数量的随机数字,返回字符串
function randnumber($number) {
mt_srand();
$randstr = ;
for(; $number > 0; $number--)
$randstr .= chr(mt_rand(48, 57));
return $randstr;
}
//生成指定范围的整数
function randinteger($min, $max) {
return (string)mt_rand($min, $max);
}
//生成指定数量的随机英文字母,type为0则大小写随机,为1大写,为2小写。返回字符串。
function randalphabet($number, $type) {
mt_srand();
$randstr = ;
loop: if($number == 0)
return $randstr;
if($type == 0) {
//大小写随机
if(mt_rand(0, 1) == 0)
goto lower;
goto upper;
}
//大写字母
if($type == 1) {
upper: $randstr .= chr(mt_rand(65, 90));
goto sub;
}
//小写字母
if ($type == 2){
lower: $randstr .= chr(mt_rand(97, 122));
} else {
return ;
}
sub: $number--;
goto loop;
}
//生成指定数量的随机字符,包括半角数字和大小写字母。返回字符串。
function randchar($number) {
mt_srand();
$randstr = ;
for(; $number > 0; $number--) {
$case = mt_rand(0, 2);
if($case == 0)
goto upper;
if($case == 1)
goto lower;
num: $randstr .= chr(mt_rand(48, 57));
continue;
upper: $randstr .= chr(mt_rand(65, 90));
continue;
lower: $randstr .= chr(mt_rand(97, 122));
}
return $randstr;
}
function randall($type, $arg) {
if($type == 0)
return randchinese($arg);
if($type == 1)
return randnumber($arg);
return randchar($arg);
}
randex.zip ( 11.04 kb 下载:5 次 )
ad:真正免费,域名+虚机+企业邮箱=0元