Php id card verification code calculation method and php verification code calculation method

Source: Internet
Author: User

Php id card verification code calculation method and php verification code calculation method

The meaning of each Chinese (Mainland) citizen ID card number is described in many articles on the Internet. The last digit of the ID card number is the verification code, which is calculated based on the first 17 digits. The algorithm is like this: multiply each number of the first 17 digits by a string of weighting factors, and then calculate the sum of these products. Use the number obtained from the product and the number obtained from the model 11 as the serial number, finally, extract the character corresponding to the serial number in a verification code string. Of course, there are also a lot of articles on the Internet to teach you how to calculate this verification code. Next we will try to use the PHP language to complete this job, maybe it can be used in PHP development, if you verify that your ID card number is correct.

Assume that the first 17 digits of the ID card number of a Chinese (Mainland) citizen are: 44010221990101001 (Note: This person was born on January 1, 2199 ), then we try to write several lines of PHP code based on the above algorithm to complete the verification code calculation. To make it easier for everyone to understand, I used simple statements. Please refer to the Code:

<? Php // the first 17 digits of the ID number, which can be obtained from various data sources (such as databases and user-submitted forms) $ body = '20160301 '; // weighting 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', '3', '2'); // process the first 17 bits for ($ I = 0; $ I <17; $ I ++) {// extract one of the first 17 digits and convert the variable type to the real number $ B = (int) $ body {$ I}; // extract the corresponding weighting factor $ w = $ wi [$ I]; // multiply the number extracted from the ID card number and the weighting factor, and accumulate $ sigma + = $ B * $ w ;} // calculate the serial number $ number = $ sigma % 11; // follow The serial number extracts the corresponding characters from the verification code string. $ Check_number = $ ai [$ number]; // output echo $ body. $ check_number;?>

After running the above code, you can calculate that the verification code for this ID card is 9. You can try the first 17 digits of your ID card.

If you understand the above example, you can combine some statements of this Code to remove unnecessary variables and optimize the Code as follows:

<?php$body = '44010221990101001';$wi = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);$ai = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');for ($i = 0;$i < 17;$i++) {$sigma += ((int) $body{$i}) * $wi[$i];}echo $body.$ai[($sigma % 11)];?>

The above is the php id card verification code calculation method introduced by xiaobian. I hope it will help you. If you have any questions, please leave a message and I will reply to you in time. Thank you very much for your support for the help House website!

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.