Home >Backend Development >PHP Problem >php generates unique strings

php generates unique strings

王林
王林Original
2019-09-20 11:54:002518browse

php generates unique strings

PHP generates non-repeating strings

Use the timestamp as the original string and randomly generate five characters Insert randomly into any position to generate a new string, ensuring no duplication.

The code is as follows:
##

function rand($len)
    {
        $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
        $string=time();
        for(;$len>=1;$len--)
        {
            $position=rand()%strlen($chars);
            $position2=rand()%strlen($string);
            $string=substr_replace($string,substr($chars,$position,1),$position2,0);
        }
        return $string;
    }

Recommended tutorial:

PHP Video tutorial

The above is the detailed content of php generates unique 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