PHP generates 8-digit non-repeating strings

angryTom
Release: 2023-02-27 12:34:01
Original
3974 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().'
'; echo randString().'
'; echo randString().'
';
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:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!