用于验证中国公民号码有效性的PHP示例代码

电脑杂谈  发布时间:2020-04-25 05:27:03  来源:网络整理

php 判断号码_ios 正则判断号_根据号判断年龄

本文将使用Java来实现对中国公民(15或18位)号码的验证php 判断号码,功能如下:

号码有效性的验证和分析. 详细的信息会生成一个虚拟的省份ID号码.

号码验证

1. 号码的结构公民识别号码是一种特征性的组合代码,由17位数字的身体代码和一位数字的校验码组成. 从左到右的排列顺序是: 六位地址码,八位出生日期码,三位顺序码和一位校验码.

ios 正则判断号_根据号判断年龄_php 判断号码

2. 地址代码(前六位数字)

指明编码对象永久居所所在县(市,旗,区)的行政区划代码,并应按照GB / T2260的规定执行.

3. 出生日期代码(七到十四位数字)

根据GB / T7408的规定,指示编码对象的出生年月日,并且在年月日代码之间没有分隔符.

ios 正则判断号_根据号判断年龄_php 判断号码

4. 序列号(第15至17位数字)

表示在由相同地址代码标识的区域内,将序列号分配给在同一年,同一月和同一天出生的人,并将序列号的奇数分配给男性,并且偶数分配给女性.

5. 验证码(第18位)

(1)十七位数字人体代码S的加权和公式= Sum(Ai * Wi),i = 0,…php 判断号码,16,首先求和前17位数字的权重

根据号判断年龄_ios 正则判断号_php 判断号码

Ai: 表示第i个位置的ID号的数字值

Wi: 表示第i个位置的加权因子Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2

(2)计算模量Y = mod(S,11)

(3)获取相应的校验码Y: 0 1 2 3 4 5 6 7 8 9 10校验码: 1 0 X 9 8 7 6 5 4 3 2

IDValidator.php

<php
namespace comjdk5logIDValidator;
 
class IDValidator {
 private static $GB2260;
 private static $instance;
 private static $cache = array();
 private static $util;
 function __construct() {
 if (!class_exists("comjdk5logIDValidatorGB2260")){
 include "GB2260.php";
 }
 if (!class_exists("comjdk5logIDValidatorutil")){
 include "util.php";
 }
 self::$GB2260 = GB2260::getGB2260 ();
 self::$util = util::getInstance();
 }
 public static function getInstance() {
 if (is_null ( self::$instance )) {
 self::$instance = new IDValidator ();
 }
 return self::$instance;
 }
 function isValid($id) {
 $code = self::$util->checkArg ( $id );
 if ($code === false) {
 return false;
 }
 // 查询cache
 if (isset ( self::$cache [ $id ] ) && self::$cache [$id] ["valid"] !== false) {
 return self::$cache [$id] ["valid"];
 } else {
 if (! isset ( self::$cache [ $id ] )) {
 self::$cache [$id] = array ();
 }
 }
 
 $addr = substr ( $code ["body"], 0, 6 );
 $birth = $code ["type"] === 18  substr ( $code ["body"], 6, 8 ) :
 substr ( $code ["body"], 6, 6 );
 $order = substr ( $code ["body"], - 3 );
 
 if (! (self::$util->checkAddr ( $addr ) && self::$util->checkBirth ( $birth ) &&
 self::$util->checkOrder ( $order ))) {
 self::$cache [$id] ["valid"] = false;
 return false;
 }
 
 // 15位不含校验码,到此已结束
 if ($code ["type"] === 15) {
 self::$cache [$id] ["valid"] = true;
 return true;
 }
 
 /* 校验位部分 */
 
 // 位置加权
 $posWeight = array ();
 for($i = 18; $i > 1; $i --) {
 $wei = self::$util->weight ( $i );
 $posWeight [$i] = $wei;
 }
 
 // 累加body部分与位置加权的积
 $bodySum = 0;
 $bodyArr = str_split( $code ["body"] );
 for($j = 0; $j < count ( $bodyArr ); $j ++) {
 $bodySum += (intval ( $bodyArr [$j], 10 ) * $posWeight [18 - $j]);
 }
 
 // 得出校验码
 $checkBit = 12 - ($bodySum % 11);
 if ($checkBit == 10) {
 $checkBit = "X";
 } else if ($checkBit > 10) {
 $checkBit = $checkBit % 11;
 }
 // 检查校验码
 if ($checkBit != $code ["checkBit"]) {
 self::$cache [$id] ["valid"] = false;
 return false;
 } else {
 self::$cache [$id] ["valid"] = true;
 return true;
 }
 }
 // 分析详细信息
 function getInfo ($id) {
 // 号码必须有效
 if ($this->isValid($id) === false) {
 return false;
 }
 // TODO 复用此部分
 $code = self::$util->checkArg($id);
 
 // 查询cache
 // 到此时通过isValid已经有了cache记录
 if (isset(self::$cache[$id]) && isset(self::$cache[$id]["info"])) {
 return self::$cache[$id]["info"];
 }
 
 $addr = substr($code["body"], 0, 6);
 $birth = ($code["type"] === 18  substr($code["body"], 6, 8) :
 substr($code["body"], 6, 6));
 $order = substr($code["body"], -3);
 
 $info = array();
 $info["addrCode"] = $addr;
 if (self::$GB2260 !== null) {
 $info["addr"] = self::$util->getAddrInfo($addr);
 }
 $info ["birth"] = ($code ["type"] === 18  (substr ( $birth, 0, 4 ) . "-" . substr ( $birth, 4, 2 ) . "-" . substr ( $birth, - 2 )) : ("19" . substr ( $birth, 0, 2 ) . "-" . substr ( $birth, 2, 2 ) . "-" . substr ( $birth, - 2 )));
 $info["sex"] = ($order % 2 === 0  0 : 1);
 $info["length"] = $code["type"];
 if ($code["type"] === 18) {
 $info["checkBit"] = $code["checkBit"];
 }
 
 // 记录cache
 self::$cache[$id]["info"] = $info;
 
 return $info;
 }
 
 // 仿造一个号
 function makeID ($isFifteen=false) {
 // 地址码
 $addr = null;
 if (self::$GB2260 !== null) {
 $loopCnt = 0;
 while ($addr === null) {
 // 防止死循环
 if ($loopCnt > 50) {
  $addr = 110101;
  break;
 }
 $prov = self::$util->str_pad(self::$util->rand(66), 2, "0");
 $city = self::$util->str_pad(self::$util->rand(20), 2, "0");
 $area = self::$util->str_pad(self::$util->rand(20), 2, "0");
 $addrTest = $prov . $city . $area;
 if (isset(self::$GB2260[$addrTest])) {
  $addr = $addrTest;
  break;
 }
 $loopCnt ++;
 }
 } else {
 $addr = 110101;
 }
 
 // 出生年
 $yr = self::$util->str_pad(self::$util->rand(99, 50), 2, "0");
 $mo = self::$util->str_pad(self::$util->rand(12, 1), 2, "0");
 $da = self::$util->str_pad(self::$util->rand(28, 1), 2, "0");
 if ($isFifteen) {
 return $addr . $yr . $mo . $da
 . self::$util->str_pad(self::$util->rand(999, 1), 3, "1");
 }
 
 $yr = "19" . $yr;
 $body = $addr . $yr . $mo . $da . self::$util->str_pad(self::$util->rand(999, 1), 3, "1");
 
 // 位置加权
 $posWeight = array();
 for ($i = 18; $i > 1; $i--) {
 $wei = self::$util->weight($i);
 $posWeight[$i] = $wei;
 }
 
 // 累加body部分与位置加权的积
 $bodySum = 0;
 $bodyArr = str_split($body);
 for ($j = 0; $j < count($bodyArr); $j++) {
 $bodySum += (intval($bodyArr[$j], 10) * $posWeight[18 - $j]);
 }
 
 // 得出校验码
 $checkBit = 12 - ($bodySum % 11);
 if ($checkBit == 10) {
 $checkBit = "X";
 } else if ($checkBit > 10) {
 $checkBit = $checkBit % 11;
 }
 return ($body . $checkBit);
 }
}

致电

<php
header("Content-type: text/html; charset=utf-8");
include "IDValidator.php";
$v = comjdk5logIDValidatorIDValidator::getInstance();
//生成一个18位号
$id = $v->makeID();
//获取信息
$info = $v->getInfo($id);
var_dump($info);
//生成一个15位号
$id = $v->makeID(true);
$info = $v->getInfo($id);
var_dump($info);
//验证号是否正确
var_dump($v->isValid("123456789012345678"));

以上是本文的全部内容,希望对大家的学习有所帮助,也希望您支持网页设计.


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-187992-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...