Because the tool needs us to write a can be random string generated function, this I think online should be a lot of this kind of generated random string function, Baidu a lot of the following I will summarize these good PHP functions for you.
Use the For loop to traverse our defined characters
The code is as follows |
Copy Code |
/* Generate Password * Length:8 */ $str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; Output Character Set $n = 8; Output string length $len = strlen ($str)-1; for ($i =0; $i < $n; $i + +) { $s. = $str [rand (0, $len)]; } Echo $s. “ ”; ?>
|
You can generate pure numeric strings of a specified length, character strings, and so on.
(uppercase, lowercase, case, and case-to-number combinations can also be expanded to suit your preferences).
Below the $length=5, if you change to 10 is 10 bits. The
Change $str = ' abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz ' to $str = ' 0123456789 ' is a pure numeric string.
The code is as follows |
Copy Code |
function Getrandstr ($length) { $str = ' abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz '; $randString = "; $len = strlen ($str)-1; for ($i = 0; $i < $length; $i + +) { $num = Mt_rand (0, $len); $randString. = $str [$num]; } return $randString; } Use the following method $test =getrandstr ($length =5); Echo $test; ?> |
or using the while
The code is as follows |
Copy Code |
/** */ function Createrandomstr ($length) { $str = ' 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ';//62 character (s) $strlen = 62; while ($length > $strlen) { $str. = $str; $strlen + = 62; } $str = Str_shuffle ($STR); Return substr ($str, 0, $length); } Echo Createrandomstr (10); ?> |
The idea of using arrays and character conversions:
The code is as follows |
Copy Code |
/** * @blog */ function Createrandomstr ($length) { $str = Array_merge (range (0,9), Range (' A ', ' Z '), Range (' A ', ' Z ')); Shuffle ($STR); $str = Implode (' ', Array_slice ($str, 0, $length)); return $str; } Echo Createrandomstr (10); ?> |
http://www.bkjia.com/PHPjc/631543.html www.bkjia.com true http://www.bkjia.com/PHPjc/631543.html techarticle because the tool needs us to write a can be random string generated function, this I think online should be a lot of this kind of generated random string function, Baidu a lot of a bunch below I give ...