How to generate non-repeating random strings in php

PHPz
Release: 2023-03-05 22:20:02
Original
4226 people have browsed it

How to generate non-repeating random strings in php: 1. Use the timestamp as the original string, then randomly generate five characters and insert them randomly into any position to generate a new string. The code is [$position=rand ()%strlen($chars)]; 2. Use the [array_slice] function to randomly extract.

How to generate non-repeating random strings in php

How to generate non-repeating random strings in php:

1. Use the timestamp as the original string, and then randomly generate five characters Randomly insert into any position and 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;
    }
Copy after login

2. Use the array_slice() function to randomly extract

<?php
$numbers = range (1,50);
//shuffle 将数组顺序随即打乱
shuffle ($numbers);
//array_slice 取该数组中的某一段
$num=6;
$result = array_slice($numbers,0,$num);
print_r($result);
?>
Copy after login

Related learning recommendations:php programming( video)

The above is the detailed content of How to generate non-repeating random strings in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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