文件名RandEx.php
namespace RandEx; include "Functions.php"; class RandEx { //构造函数,format为生成随机字符串的格式,rule为用户自定义的生成规则,const为用户自定义的常量 public function __construct($format = null, $rules = null, $const = "") { $this->format = 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 < 1) return $this->format; for($i = 0; $i < $number; $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 < $arrCount; $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 < $arrCount; $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 < $c; $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 < $loop; $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*}", "<br>", $str); $c = count($this->const[0]); for($i = 0; $i < $c; $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) < 2) 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 < 4; $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
<?php // 从一个字符串中取出两个子字符串中间的字符串 function strBetween($str, $left, $right) { $leftPos = strpos($str, $left); $leftLen = strlen($left); $rightPos = strpos($str, $right, $leftPos + $leftLen); if($leftPos === false || $rightPos === false) return ""; return substr($str, $leftPos + $leftLen, $rightPos - $leftPos - $leftLen); } // str_replace的仅替换一次的版本 function str_replace_once($search, $replace, $subject) { $pos = strpos($subject, $search); if($pos === false) return $subject; return substr_replace($subject, $replace, $pos, strlen($search)); } // 生成指定数量的随机汉字,返回字符串 function randChinese($number) { mt_srand(); $randStr = ""; for(; $number > 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); }