Home  >  Article  >  Backend Development  >  How to generate random non-repeating strings in php

How to generate random non-repeating strings in php

coldplay.xixi
coldplay.xixiOriginal
2020-08-17 09:52:393566browse

php method to randomly generate non-repeating strings: first create a php sample file; then use the timestamp as the original string; then randomly generate five characters and randomly insert them at any position; finally generate a new The string can be.

How to generate random non-repeating strings in php

php method to randomly generate non-repeating strings:

Use the timestamp as the original string, and then randomly Generate five characters and insert them randomly into any position to generate a new string, ensuring no repetition

   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;
     }

Related learning recommendations:php programming(video)

The above is the detailed content of How to generate random non-repeating strings in php. 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