php 驗證港澳臺身份證
阿新 • • 發佈:2021-09-04
/** * 驗證其他身份證號,港澳臺身份證 * @param $IDCard * @return bool */ public static function checkOtherIDCard($IDCard) { $IDCard = strtoupper($IDCard); $IDCard = str_replace(array('(', ')'), array('(', ')'), $IDCard); preg_match('/^([A-Z])([\d]{6})\(([A\d])\)$/', $IDCard, $hongkong);//香港 if ($hongkong && count($hongkong) === 4) { $sum = (ord($hongkong[1]) - 64) * 8; $index = 7; for ($j = 0; $j < 6; $j++) { $sum += $hongkong[2]{$j} * $index; $index--; } $get_num = $sum % 11; if ($get_num === 1) { $get_num = 'A'; } elseif ($get_num > 1) { $get_num = 11 - $get_num; } if ($hongkong[3] === $get_num) { return true; } return false; } preg_match('/^([A-Z])([\d]{9})$/', $IDCard, $taiwan);//中國臺灣省 if ($taiwan && count($taiwan) === 3)//首位數字代表性別,男性為1、女性為2 { $area_code = array('A' => 10, 'B' => 11, 'C' => 12, 'D' => 13, 'E' => 14, 'F' => 15, 'G' => 16, 'H' => 17, 'I' => 34, 'J' => 18, 'K' => 19, 'L' => 20, 'M' => 21, 'N' => 22, 'O' => 35, 'P' => 23, 'Q' => 24, 'R' => 25, 'S' => 26, 'T' => 27, 'U' => 28, 'V' => 29, 'W' => 32, 'X' => 30, 'Y' => 31, 'Z' => 33); $code = $area_code[$taiwan[1]]; $sum = $code{0} + $code{1} * 9; $index = 8; for ($k = 1; $k < 8; $k++) { $sum += $taiwan[2]{$k} * $index; $index--; } $get_num = $sum % 10; if ($get_num === $taiwan[2]{8}) { return true; } return false; } preg_match('/^[157][\d]{6}\([\d]\)$/', $IDCard, $aomen);//澳門 if ($aomen) { return true; } return false; }