PHP implementation generates a unique number (36 binary non-repeating number) _php instance

Source: Internet
Author: User
Tags md5 encryption
When we want to number a large amount of data, and the number has a number of digits limit, such as 5-digit license plate number, 10-bit ID number, order serial number, short URL, etc., we can use the 36 binary to calculate the number of non-duplicated digits.

We will 0-z (0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ) represent the value 0-35, such as the letter Z for 35. So I'm going to get a 5 digit number, the maximum amount of information is 36 of 5 square, 36^5 = 60466176, that is, the largest 5-bit number is equivalent to 10 decimal number: 60466176.
In order to do the demonstration, we assume that a club issued a group of 10-digit membership card number, the membership card number is 3 City number + 5-digit card number code + 2-bit check code composition. City number with the area code, such as 755 for Shenzhen, 5-digit card number is 36 of the card number of the system, the following two-bit check code is generated by a certain algorithm, the usefulness of the check code is to verify the legitimacy of the card number. In this case, the 10-bit card number we generate is equivalent to the maximum number of more than 60 million membership cards, and the unique card number is not duplicated.

PHP implementation

We use PHP for the binary conversion, 10 binary to 36 binary.

Copy CodeThe code is as follows:
Class Code {
Password dictionaries
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; Binary 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; Binary number
Key value Exchange
$dedic = Array_flip ($dics);
Go to 0
$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, define the password dictionary, that is, the value corresponding to the 0-z, the method Encodeid ($int, $format) parameter $int represents a number, $format represents the length of the bits, for example Encodeid (123456789,5) Represents a 36-digit number that converts the number 123456789 to a 5-bit, and the method Decodeid ($ids) is used to convert the 36-decimal number into a 10-binary number.
We can do this to generate the card number:
Copy the Code code as follows:
$code = new Code ();
$card _no = $code->encodeid (888888,5);

As above, we can get a 5-bit card number, which actually represents the card number is 888888 (6 8) of the membership number, and the actual conversion is 5 digit number: 0J1VC.
Then, we add the city number and check code, the city number is already defined, check code through a certain algorithm obtained, in this case, we use a simple algorithm: the first three city number and five-digit card number for MD5 encryption, and then take the MD5 value of the first 2 bits as a check code, This gives the two-digit check code that follows the number.

Copy the Code code as follows:
$card _pre = ' 755 ';
$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 10 decimal numbers from the database, guarantee the unique number, and then combine the above code, resulting in a 10-bit non-repeating membership card number.

  • Related Article

    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.