Home >Backend Development >PHP Problem >PHP generates 8-digit non-repeating strings
php generates an 8-digit non-repeating string
We will encounter random numbers when making web pages. To meet the demand for random strings, here is a method to generate a random 8-digit non-repeating string:
function randString() { $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $rand = $code[rand(0,25)] .strtoupper(dechex(date('m'))) .date('d').substr(time(),-5) .substr(microtime(),2,5) .sprintf('%02d',rand(0,99)); for( $a = md5( $rand, true ), $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV', $d = '', $f = 0; $f < 8; $g = ord( $a[ $f ] ), $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ], $f++ ); return $d; }
Use
echo randString().'<br>'; echo randString().'<br>'; echo randString().'<br>';
Result
RAUHT68I PMI8FO50 O6KFACQ8
For more PHP related knowledge, please visit PHP Chinese website!
The above is the detailed content of PHP generates 8-digit non-repeating strings. For more information, please follow other related articles on the PHP Chinese website!