Method One: Shuffle function (scrambled array) and Mt_rand function (generate random number, four times times faster than Rand)
1 /**2 * Get random string3 * @param the length $len need4 * @param $special need special symbols5 * @return String returns a random string6 */7 functionGETRANDOMSTR ($len,$special=true){8 $chars=Array(9"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",Ten"L", "M", "N", "O", "P", "Q", "R", "s", "T", "U", "V", One"W", "X", "Y", "Z", "A", "B", "C", "D", "E", "F", "G", A"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", -"S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", -"3", "4", "5", "6", "7", "8", "9" the ); - - if($special){ - $chars=Array_merge($chars,Array( +"!", "@", "#", "$", "?", "|", "{", "/", ":", ";", -"%", "^", "&", "*", "(", ")", "-", "_", "[", "]", +"}", "<", ">", "~", "+", "=", ",", "." A )); at } - - $charsLen=Count($chars)-1; - Shuffle($chars);//Scrambled Array Order - $str= ' '; - for($i= 0;$i<$len;$i++){ in $str.=$chars[Mt_rand(0,$charsLen)];//randomly take out a bit - } to return $str; +}
Method Two, Str_shuffle function (scrambled string order) and Mt_rand function
1 // take a random 10-bit string 2 $strs= "qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm"; 3 $name=substr(str_shuffle($strs),Mt_rand(0,strlen ($strs) -11); 4 Echo $name;
Method three, MD5 (), uniqid (), microtime () generate a unique 32-bit string
$uniqid MD5 (uniqid(microtime(true),true)); // Microtime (True) returns the number of milliseconds of the system's current timestamp
Other methods:
1 /**2 * Method One: Get random string3 * @param number $length length4 * @param string $type type5 * @param number $convert convert case6 * @return string random strings7 */8 functionRandom$length= 6,$type= ' String ',$convert= 0)9 {Ten $config=Array( One' Number ' = ' 1234567890 ', A' Letter ' = ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz ', -' String ' = ' abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789 ', -' All ' = ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ' the ); - - if(!isset($config[$type])) - $type= ' String '; + $string=$config[$type]; - + $code= ' '; A $strlen=strlen($string)-1; at for($i= 0;$i<$length;$i++) { - $code.=$string{Mt_rand(0,$strlen)}; - } - if(!Empty($convert)) { - $code= ($convert> 0)?Strtoupper($code) :Strtolower($code); - } in return $code; - } to + /** - * Method Two: Get random string the * @param int $randLength length * * @param int $addtime Whether to join the current timestamp $ * @param int $includenumber Whether it contains a numberPanax Notoginseng * @return String - */ the functionRAND_STR ($randLength= 6,$addtime= 1,$includenumber= 0) + { A if($includenumber) { the $chars= ' abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQEST123456789 '; +}Else { - $chars= ' ABCDEFGHIJKLMNOPQRSTUVWXYZ '; $ } $ $len=strlen($chars); - $randStr= ' '; - for($i= 0;$i<$randLength;$i++) { the $randStr.=$chars[Mt_Rand(0,$len-1)]; - }Wuyi $tokenvalue=$randStr; the if($addtime) { - $tokenvalue=$randStr. Time(); Wu } - return $tokenvalue; About}
Several methods of getting random string in PHP