Home > Backend Development > PHP Problem > How to randomly generate non-repeating letters in php

How to randomly generate non-repeating letters in php

藏色散人
Release: 2023-03-17 13:54:02
Original
2383 people have browsed it

How to randomly generate non-repeating letters in php: 1. Create a PHP sample file; 2. Use the for statement to loop from 0 to 5000. Each time a number is looped, a 12-digit number composed of letters is generated. Random code; 3. Randomly replace the numbers in the same order with the random code of letters.

How to randomly generate non-repeating letters in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

How to randomly generate non-repeating letters in php?

Random code is a randomly generated sequence of characters, which is used in many places in web design. In PHP, how to generate a large number of non-repeating random codes? For example, generate 5,000 12-digit random codes composed of alphanumeric characters, and these 5,000 random codes cannot be repeated.

There are many ways to generate specific numbers. The method used here is the method of unique numerical random letters. The specific method is: loop from 0 to 5000, and each time a number is cycled, a 12-digit random number composed of letters is generated. code, and then randomly replace the numbers in the same sequence into the random code of letters.

The attached code is as follows:

$activecode = array();
$len = 12;
$str = ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’;
for($i=0;$i<5000;$i++){
    $code = str_shuffle($str);
    $code = substr($code, 0, $len);
    $num = strval($i);
    $num_len = strlen($num);
    $p = -1;
    for($j=0;$j<$num_len;$j++){
        $p = rand($p+1, ($len-1)-($num_len-$j-1));
        $code[$p] = $num[$j];
    }
    $activecode[] = $code;
}
print_r($activecode);
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to randomly generate non-repeating letters 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