Generate a unique number in PHP (the 36-digit serial number is not repeated)

Source: Internet
Author: User

When we want to numbers a large number of data, there is a limit on the number of digits, such as a five-digit license plate number, a ten-digit license number, order serial number, and a short URL, we can use the 36-digit system to calculate non-repeated numbers that match the number of digits.

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.

Copy codeThe 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 = bcdiv ($ 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:
Copy codeThe 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.

Copy codeThe 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.