This article to: The new Too Current network blog
/** * [Generate random string] * @e-mial [email protected] * @TIME 2017-04-07 * @WEB http://blog.iinu.com.cn * @param integer $leng th [generated length] * @param integer $type [generated type] * @return [type] str [description] * @php random code type: 0, digit + capital letter, 1, number; 2, Small Letter ; 3, capital letter, 4, special character;-1, digit + case letter + special Character */functionRandcode($length= 5,$type= 0) {$arr=Array(1 = "0123456789", 2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 4 = "[Email protected]#$%^&* () {}[]|")); If ($type== 0) {Array_pop($arr);$string=Implode("",$arr); } Else If ($type== "-1") {$string=Implode("",$arr); } Else {$string=$arr[$type]; }$count=Strlen($string) - 1; For ($i= 0;$i<$length;$i++) {$str[$i] =$string[Rand(0,$count)];$code.=$str[$i]; } Return$code;}/** * [Generate unique string] * @e-mial [email protected] * @TIME 2017-04-07 * @WEB http://blog.iinu.com.cn * @ 0-stored numeric string; 1-lowercase letter string; 2-Uppercase female string; 3-Case numeric string; 4-character; * 5-number, lowercase, uppercase, character mix * @param integer $type [Type of string] * @param integer $length [Length of string] * @param integer $time [whether with time 1-band, 0-without] * @return [string] $str [Return unique string] */functionRandsole($type= 0,$length= 18,$time=0){$str=$time== 0 ? ‘‘:Date(' Ymdhis ',Time()); Switch ($type) { Case 0: For((Int)$i= 0;$i<=$length;$i++){ If(Mb_strlen($str) ==$length){$str=$str; }Else{$str.=Rand(0,9); } } Break; Case 1: For((Int)$i= 0;$i<=$length;$i++){ If(Mb_strlen($str) ==$length){$str=$str; }Else{$rand= "QWERTYUIOPLKJHGFDSAZXCVBNM";$str.=$rand{Mt_rand(0,26)}; } } Break; Case 2: For((Int)$i= 0;$i<=$length;$i++){ If(Mb_strlen($str) ==$length){$str=$str; }Else{$rand= "QWERTYUIOPLKJHGFDSAZXCVBNM";$str.=$rand{Mt_rand(0,26)}; } } Break; Case 3: For((Int)$i= 0;$i<=$length;$i++){ If(Mb_strlen($str) ==$length){$str=$str; }Else{$rand= "123456789qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM";$str.=$rand{Mt_rand(0,35)}; } } Break; Case 4: For((Int)$i= 0;$i<=$length;$i++){ If(Mb_strlen($str) ==$length){$str=$str; }Else{$rand= "[Email protected]#$%^&* () _+=-~ '";$str.=$rand{Mt_rand(0,17)}; } } Break; Case5: for((int)$i=0;$i<=$length;$i++) {if(Mb_strlen($str)==$length){$str=$str;}Else{$rand="[Email protected]#$%^&* () _+=-~ '";$str.=$rand{Mt_rand(0, the)};}} Break;}return$str;}
This article to: The new Too Current network blog
PHP generates random or unique strings