1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
| /**
* 身份证验证
* @param $id_card
*
* @return bool
*/
function check_id_card($id_card)
{
$id_card = strtoupper($id_card);
//验证有效性
$pattern = '/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/';
$passed = preg_match($pattern, $id_card);
if ($passed == 0) {
return (bool) $passed;
}
if (strlen($id_card) == 15) {
return true;
}
//验证校验位
$a = str_split($id_card, 1);
$w = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
$c = array(1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2);
$sum = 0;
for ($i = 0; $i < 17; $i++) {
$sum += $a[$i] * $w[$i];
}
return $c[$sum % 11] == $a[17];
}
|