Home > Article > Backend Development > Summary of some methods for randomly generating strings in PHP_PHP Tutorial
I have talked about generating random passwords before. Now I will introduce to you some commonly used functions for generating random strings. These are our custom functions. Of course, there are also functions built into the system, but they are relatively simple. .
mt_rand function
Example
In this case, we return some random numbers:
The code is as follows | Copy code | ||||||||
echo(mt_rand()); echo(mt_rand(10,100)); ?> The output is similar to: 3150906288 513289678 35
|
Let’s take a look at an example of the mt_rand function.
The code is as follows | Copy code | ||||
return mt_rand(1,6); } echo roll(); function roll ($sides) { return mt_rand(1,$sides); } echo roll(6); // roll a six-sided die echo roll(10); // roll a ten-sided die echo roll(20); // roll a twenty-sided die |
The code is as follows | Copy code |
function genRandomString($len) { $chars = array( "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "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" ); $charsLen = count($chars) - 1; shuffle($chars); //Shuffle the array $output = ""; for ($i=0; $i<$len; $i++) { $output .= $chars[mt_rand(0, $charsLen)]; } Return $output; } $str = genRandomString(25); $str .= " "; $str .= genRandomString(25); $str .= " "; $str .= genRandomString(25); $str .= " "; echo $str; ?> The program output is as follows: DmLVAmDkEJz8wHXRCNwzvANlB BILZSA19YyuSVcR17KrrZsOKO inlWlQF0GSabN3l589y9s16Gg |
Example
The length of the random string generated by default is 5, and the generated string contains: numbers + uppercase letters
Function:
1. Generate a random string of specified length
2. Flexibly select the complexity of the generated random string
The code is as follows
| Copy code
| ||||
/**
|
* @param string $type Random code type: 0, numbers + uppercase letters; 1, numbers; 2, lowercase letters; 3, uppercase letters; 4, special characters; -1, numbers + uppercase and lowercase letters + special characters
The code is as follows<🎜> | Copy code<🎜> <🎜> |
<🎜>function make_password( $length = 8 )<🎜> {<🎜> // Password character set, you can add any characters you need <🎜> $chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', <🎜> 'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's', <🎜> 't', 'u', 'v', 'w', 'x', 'y','z', 'A', 'B', 'C', 'D', <🎜> 'E', 'F', 'G', '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', '!', <🎜> '@','#', '$', '%', '^', '&', '*', '(', ')', '-', '_', <🎜> '[', ']', '{', '}', '<', '>', '~', '`', '+', '=', ',', '.', ';', ':', '/', '?', '|'); // Randomly select $length array element key names in $chars $keys = array_rand($chars, $length); $password = ''; for($i = 0; $i < $length; $i++) { // Concatenate $length array elements into string $password .= $chars[$keys[$i]]; } return $password; } |