根据用户指定的格式和规则生成随机字符串,支持多层嵌套,支持常量转换。附件中包含使用实例和默认的规则文件。
RandEx.php<?php <br />
namespace RandEx;<br>
include "Functions.php";<br>
class RandEx {<br>
//构造函数,format为生成随机字符串的格式,rule为用户自定义的生成规则,const为用户自定义的常量<br>
public function __construct($format = null, $rules = null, $const = "") {<br>
$this->format = is_null($format) ? "" : $format;<br>
if(is_null($rules))<br>
//如果不指定自定义规则,则读取目录下的RulesDefault.ini作为规则<br>
$rules = file_get_contents(__DIR__."/RulesDefault.ini");<br>
$this->config($rules, $const);<br>
}<br>
//设置格式($type == 0)、规则(1)、常量(2)<br>
public function set($str = null, $type) {<br>
switch($type) {<br>
case 0:<br>
$this->format = is_null($str) ? "" : $str;<br>
break;<br>
case 1:<br>
if(is_null($str))<br>
$str = file_get_contents("RulesDefault.ini");<br>
$this->config($str);<br>
break;<br>
case 2:<br>
$this->config(null, $str);<br>
break;<br>
}<br>
}<br>
//根据已配置的规则生成随机字符串,number为生成的数量(默认为1)。若不为1,返回二维数组。<br>
public function generate($number = 1) {<br>
if($number
return $this->format;<br>
for($i = 0; $i
$str = $this->format;<br>
//每次替换字符串后进行一次判断,若字符串内容无更改,则结束循环<br>
do {<br>
$tempStr = $str;<br>
$str = $this->formatConvert($str);<br>
$str = $this->constConvert($str);<br>
} while($tempStr !== $str);<br>
if($number == 1)<br>
return $str;<br>
$ret[] = $str;<br>
}<br>
return $ret;<br>
}<br>
//析构函数<br>
public function __destruct() {}<br>
//解析规则,以多维数组形式保存在私有成员变量中<br>
private function config($rules = null, $const = null) {<br>
//使用换行符分割<br>
if(!is_null($rules)) {<br>
unset($this->rules);<br>
$rulesArr = explode("\r\n", $rules);<br>
$arrCount = count($rulesArr);<br>
if($arrCount == 0)<br>
$this->rules = array(array(), array(array()));<br>
for($i = 0; $i
$tempArr = explode("::", $rulesArr[$i], 2);<br>
if(count($tempArr) != 2)<br>
continue;<br>
$this->rules[0][] = $tempArr[0];<br>
$this->rules[1][] = explode(",", $tempArr[1]);<br>
}<br>
}<br>
if(!is_null($const)) {<br>
unset($this->const);<br>
$constArr = explode("\r\n", $const);<br>
$arrCount = count($constArr);<br>
if($arrCount == 0)<br>
$this->const = array(array(), array());<br>
for($i = 0; $i
$tempArr = explode("::", $constArr[$i], 2);<br>
if(count($tempArr) != 2)<br>
continue;<br>
$this->const[0][] = $tempArr[0];<br>
$this->const[1][] = $tempArr[1];<br>
}<br>
}<br>
}<br>
//使用指定规则规则进行字符串转换<br>
private function formatConvert($str) {<br>
$c = count($this->rules[0]);<br>
$str = self::innerConvert($str);<br>
for($i = 0; $i
$find = "[*".$this->rules[0][$i].":";<br>
while(strpos($str, $find) !== false) {<br>
$replaceCount = count($this->rules[1][$i]);<br>
$rangeStr = strBetween($str, $find, "*]");<br>
$rangeArr = explode(",", $rangeStr, 2);<br>
if(count($rangeArr) != 2)<br>
break;<br>
mt_srand();<br>
//生成的次数<br>
$loop = mt_rand($rangeArr[0], $rangeArr[1]);<br>
$tempStr = "";<br>
for($j = 0; $j
$tempStr .= $this->rules[1][$i][mt_rand(0, $replaceCount - 1)];<br>
$str = str_replace_once($find.$rangeStr."*]", $tempStr, $str);<br>
}<br>
}<br>
return $str;<br>
}<br>
//转换常量<br>
private function constConvert($str) {<br>
//转换为逗号<br>
$str = str_replace("{*C*}", ",", $str);<br>
//转换为换行符,可根据情况改为\n、\r、\r\n<br>
$str = str_replace("{*B*}", "<br>", $str);<br>
$c = count($this->const[0]);<br>
for($i = 0; $i
$find = "{*".$this->const[0][$i]."*}";<br>
$str = str_replace($find, $this->const[1][$i], $str);<br>
}<br>
return $str;<br>
}<br>
//使用内置的规则进行字符串转换<br>
private static function innerConvert($str) {<br>
mt_srand();<br>
//随机英文字母<br>
while(strpos($str, "[*A:") !== false) {<br>
$result = strBetween($str, "[*A:", "*]");<br>
$arr = explode(",", $result, 3);<br>
if(count($arr)
break;<br>
if(count($arr) == 2)<br>
$arr[] = "0";<br>
$replace = randAlphabet(mt_rand((int)$arr[0], (int)$arr[1]), (int)$arr[2]);<br>
$str = str_replace_once("[*A:".$result."*]", $replace, $str);<br>
}<br>
//随机汉字、数字、字母+数字<br>
for($i = 0; $i
$prefix = array("[*C:", "[*N:", "[*S:", "[*I:");<br>
while(strpos($str, $prefix[$i]) !== false) {<br>
$result = strBetween($str, $prefix[$i], "*]");<br>
$arr = explode(",", $result, 2);<br>
if(count($arr) != 2)<br>
break;<br>
if($i == 3)<br>
$replace = randInteger((int)$arr[0], (int)$arr[1]);<br>
else<br>
$replace = randAll($i, mt_rand((int)$arr[0], (int)$arr[1]));<br>
$str = str_replace_once($prefix[$i].$result."*]", $replace, $str);<br>
}<br>
}<br>
//获取系统时间<br>
while(strpos($str, "[*D:") !== false) {<br>
$result = strBetween($str, "[*D:", "*]");<br>
$replace = date($result);<br>
$str = str_replace("[*D:".$result."*]", $replace, $str);<br>
}<br>
return $str;<br>
}<br>
private $format;<br>
private $rules;<br>
private $const;<br>
}
Functions.php<?php <br />
//从一个字符串中取出两个子字符串中间的字符串<br>
function strBetween($str, $left, $right) {<br>
$leftPos = strpos($str, $left);<br>
$leftLen = strlen($left);<br>
$rightPos = strpos($str, $right, $leftPos + $leftLen);<br>
if($leftPos === false || $rightPos === false)<br>
return "";<br>
return substr($str, $leftPos + $leftLen, $rightPos - $leftPos - $leftLen);<br>
}<br>
//str_replace的仅替换一次的版本<br>
function str_replace_once($search, $replace, $subject) {<br>
$pos = strpos($subject, $search);<br>
if($pos === false)<br>
return $subject;<br>
return substr_replace($subject, $replace, $pos, strlen($search));<br>
}<br>
//生成指定数量的随机汉字,返回字符串<br>
function randChinese($number) {<br>
mt_srand();<br>
$randStr = "";<br>
for(; $number > 0; $number--)<br>
//取随机GBK中文<br>
$randStr .= chr(mt_rand(176, 218)).chr(mt_rand(176, 218));<br>
//转换为UTF-8<br>
$randStr = iconv("GB2312", "UTF-8", $randStr);<br>
return $randStr;<br>
}<br>
//生成指定数量的随机数字,返回字符串<br>
function randNumber($number) {<br>
mt_srand();<br>
$randStr = "";<br>
for(; $number > 0; $number--)<br>
$randStr .= chr(mt_rand(48, 57));<br>
return $randStr;<br>
}<br>
//生成指定范围的整数<br>
function randInteger($min, $max) {<br>
return (string)mt_rand($min, $max);<br>
}<br>
//生成指定数量的随机英文字母,type为0则大小写随机,为1大写,为2小写。返回字符串。<br>
function randAlphabet($number, $type) {<br>
mt_srand();<br>
$randStr = "";<br>
loop: if($number == 0)<br>
return $randStr;<br>
if($type == 0) {<br>
//大小写随机<br>
if(mt_rand(0, 1) == 0)<br>
goto lower;<br>
goto upper;<br>
}<br>
//大写字母<br>
if($type == 1) {<br>
upper: $randStr .= chr(mt_rand(65, 90));<br>
goto sub;<br>
}<br>
//小写字母<br>
if ($type == 2){<br>
lower: $randStr .= chr(mt_rand(97, 122));<br>
} else {<br>
return "";<br>
}<br>
sub: $number--;<br>
goto loop;<br>
}<br>
//生成指定数量的随机字符,包括半角数字和大小写字母。返回字符串。<br>
function randChar($number) {<br>
mt_srand();<br>
$randStr = "";<br>
for(; $number > 0; $number--) {<br>
$case = mt_rand(0, 2);<br>
if($case == 0)<br>
goto upper;<br>
if($case == 1)<br>
goto lower;<br>
num: $randStr .= chr(mt_rand(48, 57));<br>
continue;<br>
upper: $randStr .= chr(mt_rand(65, 90));<br>
continue;<br>
lower: $randStr .= chr(mt_rand(97, 122));<br>
}<br>
return $randStr;<br>
}<br>
function randAll($type, $arg) {<br>
if($type == 0)<br>
return randChinese($arg);<br>
if($type == 1)<br>
return randNumber($arg);<br>
<br>
return randChar($arg);<br>
}
RandEx.zip ( 11.04 KB 下载:5 次 )
AD:真正免费,域名+虚机+企业邮箱=0元