China (mainland) Citizen ID card number each one represents the meaning, many articles on the internet have been introduced, there is not much to say. The last digit of the identity card number is the check code, which is calculated according to the first 17 digits. The algorithm is like this: the first 17 bits of each number and a series of weighting factors, and then calculate the sum of these products, the sum of these products and modulo 11 as a sequence number, and finally in a check code string to extract the character corresponding to the sequence number. Of course, there are many articles on the internet to teach you to calculate this check code, below we will try to use the PHP language to do this work, perhaps can be used in PHP development, such as verifying the user's identity card number is correct.
Suppose that the first 17 digits of a Chinese citizen's ID number are this: 44010221990101001 (note: This person was born in 2199), then we followed the above algorithm to write a few lines of PHP code to complete the verification code calculation. To make it easier for everyone to understand, I used a simpler statement, see the code:
<?php//ID number top 17, can be obtained from various data sources (such as database, user submitted form, etc.) $body = ' 44010221990101001 ';//weighted factor $wi = Array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);//Check code string $ai = Array (' 1 ', ' 0 ', ' X ', ' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ');//sequentially cycle through the first 3 bits for ($i = 0;$ I < $i + +) {//extract one of the first 17 bits and convert the variable type to real $b = (int) $body {$i};//extract the corresponding weighting factor $w = $wi [$i];//multiplies a number and weighting factor extracted from the ID number and accumulates $ Sigma + = $b * $W;} Calculated ordinal # = $sigma% 11;//extracts the corresponding characters from the checksum string by ordinal. $check _number = $ai [$number];//output echo $body. $check _number;? >
After running the above code, you can figure out that the ID of the check code is 9. You can try the top 17 of your ID card.
If you understand the above example, you can merge some of the statements of this code, remove unnecessary variables, and optimize for the following code:
<?php$body = ' 44010221990101001 '; $wi = Array (7, 9, 5, 8, 4, 2, 1, 6, 3, 7, 9, ten, 5, 8, 4, 2); $ai = Array (' 1 ', ' 0 ', ' X ', ' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 ', ' 2 '); for ($i = 0; $i <; $i + +) {$sigma + = ((int) $body {$i}) * $wi [$i];} echo $body. $ai [($sigma% 11)];? >
The above is a small series to introduce you to the PHP ID code calculation method, I hope we have some help, if you have any questions please give me a message, small series will promptly reply to you. Thank you very much for the support of the Scripting House website!