介绍2种方法 , 第一种是我获取远程的网页数据 , 然后正则处理, 返回你需要的数据, 你可以直接复制, 参数替换好打印
$IDNumber $judge $NativePlace $birth $Gender 这几个参数看看
实例
$url = " //$身份证替换你自己变量 $list = file_get_contents("compress.zlib://".$url); $list= mb_convert_encoding($list, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5'); $res = preg_replace("/(\s|\ \;| |\xc2\xa0)/", " ", strip_tags($list)); $IDNumber = substr($res,strripos($res,"证件号码")+12, 18); $judge = substr($res,strripos($res,"判断参考")+12, 36); $NativePlace = substr($res,strripos($res,"发 证 地")+11, 9); $birth = substr($res,strripos($res,"出生日期")+12, 19); $Gender = substr($res,strripos($res,"性别年龄")+12,12 ); if ($judge === "身份证号码校验为合法号码") $status = true; else $status = false; if ($status) { $res = Db::name('user')->where('id',$userid)->update([ 'identityCard' => $IDNumber, 'fullname' => $name, ]); if ($res) return json([ 'code' => 200, 'data' => [], 'msg' => "验证成功" ]);else return json([ 'code' => 200, 'data' => [], 'msg' => "验证成功, 但保存数据失败" ]);
运行实例 »
点击 "运行实例" 按钮查看在线实例
另一种就是正则匹配 , 只会返回布尔值
实例
/** * 判断是否为合法的身份证号码 * @param $mobile * @return int */ function isCreditNo($vStr){ $vCity = array( '11','12','13','14','15','21','22', '23','31','32','33','34','35','36', '37','41','42','43','44','45','46', '50','51','52','53','54','61','62', '63','64','65','71','81','82','91' ); if (!preg_match('/^([\d]{17}[xX\d]|[\d]{15})$/', $vStr)) return false; if (!in_array(substr($vStr, 0, 2), $vCity)) return false; $vStr = preg_replace('/[xX]$/i', 'a', $vStr); $vLength = strlen($vStr); if ($vLength == 18) { $vBirthday = substr($vStr, 6, 4) . '-' . substr($vStr, 10, 2) . '-' . substr($vStr, 12, 2); } else { $vBirthday = '19' . substr($vStr, 6, 2) . '-' . substr($vStr, 8, 2) . '-' . substr($vStr, 10, 2); } if (date('Y-m-d', strtotime($vBirthday)) != $vBirthday) return false; if ($vLength == 18) { $vSum = 0; for ($i = 17 ; $i >= 0 ; $i--) { $vSubStr = substr($vStr, 17 - $i, 1); $vSum += (pow(2, $i) % 11) * (($vSubStr == 'a') ? 10 : intval($vSubStr , 11)); } if($vSum % 11 != 1) return false; } return true; }