-
- function randomkeys($length)
- {
- $pattern = '1234567890abcdefghijklmnopqrstuvwxyz
- ABCDEFGHIJKLOMNOPQRSTUVWXYZ,./&l
- t;>?;#:@~[]{ }-_=+) (*&^%___FCKpd___0pound;”!'; //Character pool
- for($i=0;$i<$length;$i++)
- {
- $key .= $pattern{mt_rand(0,35)}; //Generate PHP random numbers
- }
- return $key;
- }
- echo randomkeys(8); This PHP random function can generate strings like Random number method: Use the chr() function to save the step of creating a character pool. function randomkeys($length)
- {
- $output=”;
- for ($a = 0; $a < $length; $a++ ) {
- $output .= chr(mt_rand(33, 126)); //Generate php random numbers
- }
- return $output;
- }
- echo randomkeys(8);
- ?>
-
Copy code Note:
In the second PHP random function, first use mt_rand() to generate a PHP random number between 33 and 126, and then use the chr() function to convert it into characters.
Looking at the ascii code table, you will find that 33 to 126 represent all the characters in the character pool in the first function. The second function has the same functionality as the first function, but is more concise. |