http://www.lmws.net/phpweb/Article.php?id=79
Use a PHP to calculate ID card check code China (mainland) Citizen ID card number, and many articles on the Internet have introduced, there are not many sayings here. The last bit of the ID number is the check code, and it is calculated according to the first 17 bits. The algorithm is probably this: multiplied each of the first 17 bits and a string weighting factor, then calculates these products; the number obtained by the sum of these products as the serial number, and finally extracts in a check code string. A character corresponding to the serial number. Of course, there are many articles on the Internet to teach you to calculate this check code. Here we will try to complete this in PHP language, maybe it can be used in PHP development, such as whether the user's ID number is correct. Assuming a Chinese (mainland) citizen ID number is 17: 4401021990101001 (Note: This person is born in 2199), then we followed a few line PHP code to complete the calculation of the check code according to the above algorithm. In order to make everyone easier to understand, I use a simple statement, please see the code:
PHP // ID number 17 digits, you can get from various data sources (such as databases, users submit forms, etc.) $ body = '44010221990101001'; // Weighted factor $ wi = array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); // Cat Check Skew $ AI = Array ('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); // Phase the first 17-bit forces according to sequential cycle ($ I = 0; $ I <17; $ i ) {// Extract one of the first 17 bits, and turn the variable type to a real $ b = (int) $ body {$ I}; // Extract the corresponding weighting factor $ w = $ wi [$ i]; // multiplied one number and weighting factor extracted from the ID card number, and add $ sigma = $ b * $ W;} // calculation serial number $ number = $ Sigma% 11; // Extract the corresponding characters from the check code string according to the serial number. $ Check_Number = $ AI [$ Number]; // Output Echo $ BODY. $ CHECK_NUMBER;?> After running the above code, you can calculate the check code of the ID card. You can try it with your identity card. If you understand the above example, you can merge some of this code, remove the necessary variables, optimize the following code: