文档名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);
}