|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Slince/China package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Slince <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace China\IDCard; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* 算法摘自 |
|
15
|
|
|
* {@link https://www.cnblogs.com/bossikill/p/3679926.html}. |
|
16
|
|
|
*/ |
|
17
|
|
|
final class IDCardUtils |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* 计算身份证最后一位. |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $IDCardBody |
|
23
|
|
|
* |
|
24
|
|
|
* @return bool|number |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function calcIDCardCode($IDCardBody) |
|
27
|
|
|
{ |
|
28
|
|
|
if (strlen($IDCardBody) != 17) { |
|
29
|
|
|
return false; |
|
30
|
|
|
} |
|
31
|
|
|
//加权因子 |
|
32
|
|
|
$factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); |
|
33
|
|
|
//校验码对应值 |
|
34
|
|
|
$code = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); |
|
35
|
|
|
$checksum = 0; |
|
36
|
|
|
|
|
37
|
|
|
for ($i = 0; $i < strlen($IDCardBody); ++$i) { |
|
38
|
|
|
$checksum += substr($IDCardBody, $i, 1) * $factor[$i]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return $code[$checksum % 11]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* 将15位身份证升级到18位. |
|
46
|
|
|
* |
|
47
|
|
|
* @param number $id |
|
48
|
|
|
* |
|
49
|
|
|
* @return bool|string |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function convertIDCard15to18($id) |
|
52
|
|
|
{ |
|
53
|
|
|
if (strlen($id) != 15) { |
|
54
|
|
|
return false; |
|
55
|
|
|
} else { |
|
56
|
|
|
// 如果身份证顺序码是996 997 998 999,这些是为百岁以上老人的特殊编码 |
|
57
|
|
|
if (array_search(substr($id, 12, 3), array('996', '997', '998', '999')) !== false) { |
|
58
|
|
|
$IDCard = substr($id, 0, 6).'18'.substr($id, 6, 9); |
|
|
|
|
|
|
59
|
|
|
} else { |
|
60
|
|
|
$IDCard = substr($id, 0, 6).'19'.substr($id, 6, 9); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $id.static::calcIDCardCode($id); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* 18位身份证校验码有效性检查. |
|
69
|
|
|
* |
|
70
|
|
|
* @param number $IDCard |
|
71
|
|
|
* |
|
72
|
|
|
* @return bool |
|
73
|
|
|
*/ |
|
74
|
|
|
public static function check18IDCard($IDCard) |
|
75
|
|
|
{ |
|
76
|
|
|
if (strlen($IDCard) != 18) { |
|
77
|
|
|
return false; |
|
78
|
|
|
} |
|
79
|
|
|
$IDCardBody = substr($IDCard, 0, 17); //身份证主体 |
|
80
|
|
|
$IDCardCode = strtoupper(substr($IDCard, 17, 1)); //身份证最后一位的验证码 |
|
81
|
|
|
if (static::calcIDCardCode($IDCardBody) != $IDCardCode) { |
|
82
|
|
|
return false; |
|
83
|
|
|
} else { |
|
84
|
|
|
return true; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.