PHP generates random or unique strings

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.