Home >Backend Development >PHP Problem >PHP generates 8-digit non-repeating strings

PHP generates 8-digit non-repeating strings

angryTom
angryTomOriginal
2019-10-19 13:23:164217browse

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().&#39;<br>&#39;;
echo randString().&#39;<br>&#39;;
echo randString().&#39;<br>&#39;;

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn