PHP generates unique membership card number
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.
?
View Demo Demos
?
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
We use PHP for the binary conversion, 10 binary to 36 binary.
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 ', 3 3=> ' X ', 34=> ' Y ', 35=> ' Z '); Public Function Encodeid ($int, $format =8) {$dics = $this->dic; $dnum = 36; Number of binary $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)); The 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]); Reversal $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:
?
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.
?
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.
?
This article originates from helloweba.com