This article mainly introduces how to generate a unique number in PHP. In this article, convert the hexadecimal value to 36 to obtain more than 60 million unique numbers that are not repeated, with the number of digits being 10 digits, if you need a large data number, you can refer to the following, for example, the five-digit license plate number, the ten-digit license number, the Order serial number, and the short website address can be calculated using the 36-digit notation.
We use 0-Z (0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ) to represent the value 0-35, for example, the letter Z represents 35. In this case, I want to get a five-digit number. The maximum amount of information is 36 to the power of 5, 36 ^ 5 = 60466176, that is, the maximum number of five digits is equivalent to a 10-digit number: 60466176.
In this article, we assume that a club issues a group of 10 member card numbers. The membership card number consists of three city numbers, five card numbers, and two verification codes. City numbers are represented by area numbers. For example, 755 represents Shenzhen, and 5-digit card numbers are composed of 36-digit card numbers. The last two verification codes are generated by a certain algorithm, the purpose of the Verification Code is to verify the validity of the card number. In this case, the 10-digit card number we generate is equivalent to a membership card number up to more than 60 million, and is unique and unique.
PHP implementation
We use PHP for hexadecimal conversion, and 10 to 36.
The Code is as follows:
Class Code {
// Password dictionary
Private $ dic = array (
0 => '0', 1 => '1', 2 => '2', 3 => '3', 4 => '4 ', 5 => '5', 6 => '6', 7 => '7', 8 => '8 ',
9 => '9', 10 => 'A', 11 => 'B', 12 => 'C', 13 => 'D ', 14 => 'E', 15 => 'F', 16 => 'G', 17 => 'h ',
18 => 'I', 19 => 'J', 20 => 'k', 21 => 'l', 22 => 'M ', 23 => 'n', 24 => 'O', 25 => 'P', 26 => 'Q ',
27 => 'R', 28 =>'s ', 29 => 'T', 30 => 'U', 31 => 'V ', 32 => 'w', 33 => 'x', 34 => 'y', 35 => 'Z'
);
Public function encodeID ($ int, $ format = 8 ){
$ Dics = $ this-> dic;
$ Dnum = 36; // hexadecimal number
$ Arr = array ();
$ Loop = true;
While ($ loop ){
$ Arr [] = $ dics [bcmod ($ int, $ dnum)];
$ Int = bcp ($ int, $ dnum, 0 );
If ($ int = '0 '){
$ Loop = false;
}
}
If (count ($ arr) <$ format)
$ Arr = array_pad ($ arr, $ format, $ dics [0]);
Return implode ('', array_reverse ($ arr ));
}
Public function decodeID ($ ids ){
$ Dics = $ this-> dic;
$ Dnum = 36; // hexadecimal number
// Key-value exchange
$ Dedic = array_flip ($ dics );
// Zero Removal
$ Id = ltrim ($ ids, $ dics [0]);
// Reverse
$ Id = strrev ($ id );
$ V = 0;
For ($ I = 0, $ j = strlen ($ id); $ I <$ j; $ I ++ ){
$ V = bcadd (bcmul ($ dedic [$ id {
$ I}
], Bcpow ($ dnum, $ I, 0), 0), $ v, 0 );
}
Return $ v;
}
}
We define the Code class. First, we define the password dictionary, that is, the values corresponding to 0-Z. In the encodeID ($ int, $ format) method, the parameter $ int represents the number, and $ format represents the length of the number of digits, for example, encodeID (123456789,5) indicates to convert the number 123456789 to a 5-digit 36-digit number, and the decodeID ($ ids) Method) it is used to convert a 36-digit number into a 10-digit number.
We can generate the card number as follows:
The Code is as follows:
$ Code = new Code ();
$ Card_no = $ code-> encodeID (888888,5 );
As shown above, we can get a 5-digit card number, which actually represents the member number whose card number is 888888 (6 8), and the actual conversion is the 5-digit number: 0J1VC.
Next, we add the city number and verification code. The city number is defined, and the verification code is obtained through a certain algorithm. In this example, we use a simple algorithm: perform md5 encryption on the first three city numbers and five card numbers, and then use the first two digits of the md5 value as the verification code. Then, the two digits after the md5 value are obtained.
The Code is as follows:
$ Card_pre = '000000 ';
$ Card_vc = substr (md5 ($ card_pre. $ card_no), 0, 2 );
$ Card_vc = strtoupper ($ card_vc );
Echo $ card_pre. $ card_no. $ card_vc;
In practice, you can get a 10-digit number through the database to ensure that the number is unique. Then, combine the above Code to generate a 10-digit non-repeated membership card number.