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

PHP generates 8-digit non-repeating strings

angryTom
Release: 2023-02-27 12:34:01
Original
4269 people have browsed it

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;
}
Copy after login

Use

echo randString().&#39;<br>&#39;;
echo randString().&#39;<br>&#39;;
echo randString().&#39;<br>&#39;;
Copy after login

Result

RAUHT68I
PMI8FO50
O6KFACQ8
Copy after login

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!

Related labels:
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template