Home >Backend Development >PHP Tutorial >Implementation of ID card verification code calculation method based on PHP

Implementation of ID card verification code calculation method based on PHP

墨辰丷
墨辰丷Original
2018-06-01 15:45:552083browse

This article will share with you an attempt to use the PHP language to implement the ID card verification code to determine whether the user's ID number is correct.

The meaning of each digit of the Chinese (Mainland) citizen ID number. Many articles on the Internet have introduced it, so I won’t go into details here. The last digit of the ID number is the check code, which is calculated based on the first 17 digits. The algorithm is roughly like this: multiply each number in the first 17 digits by a series of weighting factors, and then calculate the sum of these products; use the sum of these products modulo 11 as the serial number, and finally extract it from a check code string Output the characters corresponding to the serial number. Of course, there are many articles on the Internet teaching you to calculate this check code. Below we will try to use PHP language to complete this work. It may be used in PHP development, such as verifying whether the user's ID number is correct.

Assume that the first 17 digits of a Chinese (Mainland) citizen’s ID card number are this: 44010221990101001 (Note: This person was born in 2199), then we try to write a few lines of PHP code according to the above algorithm to complete Calculation of checksum. In order to make it easier for everyone to understand, I used simpler statements, please look at the code:

<?php
//身份证号码前17位,可以从各种数据源中获得(如数据库、用户提交的表单等)
$body = &#39;44010221990101001&#39;;
//加权因子
$wi = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
//校验码串
$ai = array(&#39;1&#39;, &#39;0&#39;, &#39;X&#39;, &#39;9&#39;, &#39;8&#39;, &#39;7&#39;, &#39;6&#39;, &#39;5&#39;, &#39;4&#39;, &#39;3&#39;, &#39;2&#39;);
//按顺序循环处理前17位
for ($i = 0;$i < 17;$i++) {
//提取前17位的其中一位,并将变量类型转为实数
$b = (int) $body{$i};
//提取相应的加权因子
$w = $wi[$i];
//把从身份证号码中提取的一位数字和加权因子相乘,并累加
$sigma += $b * $w;
}
//计算序号
$number = $sigma % 11;
//按照序号从校验码串中提取相应的字符。
$check_number = $ai[$number];
//输出
echo $body.$check_number;
?>

After running the above code, you can calculate the number of the ID card The check code is 9. You can try using the first 17 digits of your ID card.

If you understand the above example, you can merge some statements of this code, remove unnecessary variables, and optimize to get the following code:

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

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Detailed examples of how to use static variables in PHP

thinkPHP’s method of implementing the order digital reminder function

Notes on accessing PHP Sesame Credit

##

The above is the detailed content of Implementation of ID card verification code calculation method based on PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn