ofcold/identity-card

中华人民共和国居民身份证的简单证明。

3.3.2 2023-07-09 01:01 UTC

README

996.icu LICENSE Total Downloads

中国(地区)公民身份证工具


简体中文文档


中国(大陆)身份证包,数据来自国家标准 GB/T 2260-2007 (中华人民共和国行政区划代码标准)。

其他语言

安装

    composer require ofcold/identity-card

说明

基于中华人民共和国公民身份证获取用户信息的一个组件。这适用于任何PHP框架,但前提是PHP版本大于7.1。

使用

在Laravel验证中使用

多语言添加 请输入有效的身份证 到您的json语言包中。

use Ofcold\IdentityCard\Rules\IdCard;

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'id_card'  => [
            'required',
            new IdCard
        ]
    ];
}

验证您的中国身份证

    // Result false OR Ofcold\IdentityCard\IdentityCard instance.
    $result = Ofcold\IdentityCard\IdentityCard::make('32010619831029081');

    if ( $result === false ) {

        return 'Your ID number is incorrect';
    }

    print_r($result->toArray());

或测试文件。

    php test
$idCard = Ofcold\IdentityCard\IdentityCard::make('320106198310290811', 'en');
//  Use locale, Current supported zh-cn,en
// $idCard = Ofcold\IdentityCard\IdentityCard::make('320106198310290811', 'zh-cn');
if ( $idCard === false ) {

    return 'Your ID number is incorrect';
}
$area = $idCard->getArea();
$gender = $idCard->getGender();
$birthday = $idCard->getBirthday();
$age = $idCard->getAge();
$constellation = $idCard->getConstellation();

结果

{
    "area": "shan xi sheng yun cheng di qu yun cheng shi",
    "province": "shan xi sheng",
    "city": "yun cheng di qu",
    "county": "yun cheng shi",
    "gender": "Male",
    "birthday": "1980-03-12",
    "zodiac": "Pig",
    "age": 38,
    "constellation": "Pisces"
}

API

  • getArea() : string 获取地区
  • getConstellation() : string 获取星座
  • getZodiac() : string 获取生肖
  • getAge() : int 获取年龄
  • getBirthday(string $foramt = 'Y-m-d') : string 获取生日
  • getGender() : string 获取性别
  • getCounty() : string|null 获取县
  • getCity() : string|null 获取市
  • getProvince() : string|null 获取省
  • toArray() : array 获取所有信息。
  • toJson(int $option) : string JSON格式所有信息
  • __get() : mixed
  • __toString() : toJson

变更日志

V3.0.0

  • 添加Laravel规则。

V2.0.0

  • 添加#2 __get()
  • 添加#2 __toString()
  • 修改#2 静态make()方法返回当前对象或布尔类型
  • 移除#2 构造方法异常验证