PHPackages                             anhao/id-validator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. anhao/id-validator

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

anhao/id-validator
==================

Chinese Mainland Personal ID Card Validation

v1.0.0(1y ago)0126MITPHPPHP &gt;=8.1

Since Dec 15Pushed 1y agoCompare

[ Source](https://github.com/anhao/id-validator)[ Packagist](https://packagist.org/packages/anhao/id-validator)[ RSS](/packages/anhao-id-validator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

IdValidator.php
===============

[](#idvalidatorphp)

**中华人民共和国居民身份证**、**中华人民共和国港澳居民居住证**以及**中华人民共和国台湾居民居住证**号码验证工具（PHP Composer 版）支持 15 位与 18 位号码。

- [Go 版本](https://github.com/guanguans/id-validator)
- [Python 版本](https://github.com/jxlwqq/id-validator.py)
- [Ruby 版本](https://github.com/renyijiu/id_validator)
- [JavaScript 版本](https://github.com/mc-zone/IDValidator)

[![Testing](https://github.com/jxlwqq/id-validator/actions/workflows/testing.yml/badge.svg)](https://github.com/jxlwqq/id-validator/actions/workflows/testing.yml)[![StyleCI](https://camo.githubusercontent.com/e56263de61c301925b18079b37e8ea4142abe283383fd89542e247119b9fc6f9/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3134373735383836322f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/147758862)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/2c2630057902f5c0f9e5b09a42b3b141042beb403114100484642b74bc210689/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a786c7771712f69642d76616c696461746f722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jxlwqq/id-validator/?branch=master)[![Maintainability](https://camo.githubusercontent.com/702508c0f76070ab5873b0afcfec536a74b8c7f6780d7c9a3b6aa5488b2172db/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f39383838306135636237313365323635323435302f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/jxlwqq/id-validator/maintainability)[![FOSSA Status](https://camo.githubusercontent.com/2cc4d05b7cc42e66093e93e0aae0c8304033ff9f03ba9756c6bf3bb996fa3fe2/68747470733a2f2f6170702e666f7373612e696f2f6170692f70726f6a656374732f6769742532426769746875622e636f6d2532466a786c77717125324669642d76616c696461746f722e7376673f747970653d736869656c64)](https://app.fossa.io/projects/git%2Bgithub.com%2Fjxlwqq%2Fid-validator?ref=badge_shield)[![996.icu](https://camo.githubusercontent.com/ac8f294a80f65338db545230f1a881b9a382204a1f187c6ff40ee679d42d40ca/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c696e6b2d3939362e6963752d7265642e737667)](https://996.icu)

安装
--

[](#安装)

```
composer require "anhao/id-validator"
```

> 注：如果 require 失败，解决方案见 [\#13](https://github.com/jxlwqq/id-validator/pull/13)。

使用
--

[](#使用)

> `440308199901101512` 和 `610104620927690` 示例大陆居民身份证均为随机生成的假数据，如撞车，请联系删除。 `810000199408230021` 和 `830000199201300022` 示例港澳台居民居住证为北京市公安局公布的居住证样式号码。

### 验证身份证号合法性

[](#验证身份证号合法性)

验证身份证号是否合法，合法返回 `true`，不合法返回 `false`：

```
use Jxlwqq\IdValidator\IdValidator;

$idValidator = new IdValidator();
$idValidator->isValid('440308199901101512'); // 大陆居民身份证 18 位
$idValidator->isValid('610104620927690');    // 大陆居民身份证 15 位
$idValidator->isValid('810000199408230021'); // 港澳居民居住证 18 位
$idValidator->isValid('830000199201300022'); // 台湾居民居住证 18 位
```

### 获取身份证号信息

[](#获取身份证号信息)

当身份证号合法时，返回分析信息（地区、出生日期、星座、生肖、性别、校验位），不合法返回 `false`：

```
use Jxlwqq\IdValidator\IdValidator;

$idValidator = new IdValidator();
$idValidator->getInfo('440308199901101512'); // 18 位
$idValidator->getInfo('610104620927690');    // 15 位
```

返回信息格式如下：

```
[
    'addressCode'   => '440308',                    // 地址码
    'abandoned'     => 0,                           // 地址码是否废弃，1 为废弃的，0 为正在使用的
    'address'       => '广东省深圳市盐田区',           // 地址
    'addressTree'   => ['广东省', '深圳市', '盐田区'], // 省市区三级列表
    'birthdayCode'  => '1999-01-10',                // 出生日期
    'constellation' => '水瓶座',                     // 星座
    'chineseZodiac' => '卯兔',                       // 生肖
    'sex'           => 1,                           // 性别，1 为男性，0 为女性
    'length'        => 18,                          // 号码长度
    'checkBit'      => '2',                         // 校验码
]
```

> 注：判断地址码是否废弃的依据是[中华人民共和国行政区划代码历史数据集](https://github.com/jxlwqq/address-code-of-china)，本数据集的采集源来自：[中华人民共和国民政部](http://www.mca.gov.cn/article/sj/xzqh//1980/)，每年更新一次。本数据集采用 csv 格式存储，方便大家进行数据分析或者开发其他语言的版本。

### 生成可通过校验的假数据

[](#生成可通过校验的假数据)

伪造符合校验的身份证：

`fakeId()` 方法有 4 个可选参数：

- `$eighteen` 是否生成 18 位号码，默认为 `true`；
- `$address` 地址，即省市县三级地区官方全称，如`北京市`、`台湾省`、`香港特别行政区`、`深圳市`、`黄浦区`等，默认或参数非法，则生成合法的随机地址；
- `$birthday` 出生日期，如 `2000`、`198801`、`19990101` 等，默认或参数非法，则生成合法的随机出生日期；
- `$sex` 性别，1 为男性，0 为女性，默认或参数非法，则生成合法的随机性别；

```
use Jxlwqq\IdValidator\IdValidator;

$idValidator = new IdValidator();
$idValidator->fakeId();                                    // 18 位
$idValidator->fakeId(false);                               // 15 位
$idValidator->fakeId(true, '上海市', '2000', 1);            // 生成出生于 2000 年上海市的男性居民身份证
$idValidator->fakeId(true, '南山区', '1999', 0);            // 生成出生于 1999 年广东省深圳市南山区的女性居民身份证
$idValidator->fakeId(true, '江苏省', '200001', 1);          // 生成出生于 2000 年 1 月江苏省的男性居民身份证
$idValidator->fakeId(true, '厦门市', '199701', 0);          // 生成出生于 1997 年 1 月福建省厦门市的女性居民身份证
$idValidator->fakeId(true, '台湾省', '20131010', 0);        // 生成出生于 2013 年 10 月 10 日台湾省的女性居民居住证
$idValidator->fakeId(true, '香港特别行政区', '19970701', 0); // 生成出生于 1997 年 7 月 1 日香港特别行政区的女性居民居住证
```

### 升级身份证号码

[](#升级身份证号码)

15 位号码升级为 18 位：

```
use Jxlwqq\IdValidator\IdValidator;

$idValidator = new IdValidator();
$idValidator->upgradeId('610104620927690'); // 15 位号码升级为 18 位
```

参考资料
----

[](#参考资料)

- [中华人民共和国公民身份号码](https://zh.wikipedia.org/wiki/%E4%B8%AD%E5%8D%8E%E4%BA%BA%E6%B0%91%E5%85%B1%E5%92%8C%E5%9B%BD%E5%85%AC%E6%B0%91%E8%BA%AB%E4%BB%BD%E5%8F%B7%E7%A0%81)
- [中华人民共和国民政部：行政区划代码](http://www.mca.gov.cn/article/sj/xzqh/)
- [中华人民共和国行政区划代码历史数据集](https://github.com/jxlwqq/address-code-of-china)
- [国务院办公厅关于印发《港澳台居民居住证申领发放办法》的通知](http://www.gov.cn/zhengce/content/2018-08/19/content_5314865.htm)
- [港澳台居民居住证](https://zh.wikipedia.org/wiki/%E6%B8%AF%E6%BE%B3%E5%8F%B0%E5%B1%85%E6%B0%91%E5%B1%85%E4%BD%8F%E8%AF%81)

Change Log
----------

[](#change-log)

- 1.1.0 身份证号返回信息新增生肖和星座内容；
- 1.2.0 支持港澳台居民居住证；
- 1.3.0 行政区划代码（地址码）数据改由从中华人民共和国民政部官方网站获取；
- 1.4.0 支持查询因行政区变更而废弃的地址码；
- 1.4.2 `fakeId()` 方法增加可选参数；
- 1.4.11 支持 15 位身份证号码升级为 18 位；
- 1.4.18 `getInfo()` 返回值新增省市区三级列表

License
-------

[](#license)

MIT

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance40

Moderate activity, may be stable

Popularity11

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 92.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

510d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/22a45c91e7343a20031eb47d6f02fcb59a5a0d5f37936124242cff4730ca5af5?d=identicon)[Alone88](/maintainers/Alone88)

---

Top Contributors

[![jxlwqq](https://avatars.githubusercontent.com/u/2421068?v=4)](https://github.com/jxlwqq "jxlwqq (158 commits)")[![samnela](https://avatars.githubusercontent.com/u/1852108?v=4)](https://github.com/samnela "samnela (5 commits)")[![guanguans](https://avatars.githubusercontent.com/u/22309277?v=4)](https://github.com/guanguans "guanguans (3 commits)")[![anhao](https://avatars.githubusercontent.com/u/9844218?v=4)](https://github.com/anhao "anhao (2 commits)")[![liuzhengjian](https://avatars.githubusercontent.com/u/4033714?v=4)](https://github.com/liuzhengjian "liuzhengjian (1 commits)")[![xjchengo](https://avatars.githubusercontent.com/u/4819996?v=4)](https://github.com/xjchengo "xjchengo (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/anhao-id-validator/health.svg)

```
[![Health](https://phpackages.com/badges/anhao-id-validator/health.svg)](https://phpackages.com/packages/anhao-id-validator)
```

###  Alternatives

[webmozart/assert

Assertions to validate method input/output with nice error messages.

7.6k894.0M1.2k](/packages/webmozart-assert)[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.9M104](/packages/bensampo-laravel-enum)[swaggest/json-schema

High definition PHP structures with JSON-schema based validation

48612.5M73](/packages/swaggest-json-schema)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[ashallendesign/laravel-config-validator

A package for validating your Laravel app's config.

217905.3k5](/packages/ashallendesign-laravel-config-validator)[crazybooot/base64-validation

Laravel validators for base64 encoded files

1341.9M8](/packages/crazybooot-base64-validation)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
