|
1 | 1 | # idcard
|
2 | 2 | 二代身份证号码验证,可用于验证二代身份证号码是否合法、从身份证号码中获取生日/性别/年龄/地区信息 A Chinese identity card number verification class
|
| 3 | + |
| 4 | +## 安装 |
| 5 | + |
| 6 | +### 使用composer安装(建议) |
| 7 | + |
| 8 | +``` |
| 9 | +# 稳定版本 |
| 10 | +composer require ionepub/idcard |
| 11 | +composer require --prefer-dist ionepub/idcard |
| 12 | +
|
| 13 | +# 开发版本 |
| 14 | +composer require ionepub/idcard:dev-master |
| 15 | +``` |
| 16 | + |
| 17 | +### 直接下载 |
| 18 | + |
| 19 | +下载地址:https://github.com/ionepub/idcard/releases |
| 20 | + |
| 21 | +## 使用 |
| 22 | + |
| 23 | +### 引入包 |
| 24 | + |
| 25 | +- composer |
| 26 | + |
| 27 | +``` |
| 28 | +require 'vendor/autoload.php'; |
| 29 | +``` |
| 30 | + |
| 31 | +- 文件引入 |
| 32 | + |
| 33 | +``` |
| 34 | +require 'idcard/src/Idcard.php'; |
| 35 | +``` |
| 36 | + |
| 37 | +### 实例化 |
| 38 | + |
| 39 | +``` |
| 40 | +use Ionepub\Idcard; |
| 41 | +$idcard = Idcard::getInstance(); |
| 42 | +``` |
| 43 | + |
| 44 | +或直接使用 |
| 45 | + |
| 46 | +``` |
| 47 | +$idcard = Ionepub\Idcard::getInstance(); |
| 48 | +``` |
| 49 | + |
| 50 | +### 设置一个身份证号 |
| 51 | + |
| 52 | +``` |
| 53 | +$idcard->setId('130724197906126153'); |
| 54 | +``` |
| 55 | + |
| 56 | +也可以在创建实例时设置: |
| 57 | + |
| 58 | +``` |
| 59 | +$idcard = Ionepub\Idcard::getInstance('130724197906126153'); |
| 60 | +``` |
| 61 | + |
| 62 | +> 设置身份证号的方法支持链式操作 |
| 63 | +
|
| 64 | +### 获取身份证号 |
| 65 | + |
| 66 | +``` |
| 67 | +$id = $idcard->getId(); |
| 68 | +``` |
| 69 | + |
| 70 | +可以跟设置连起来用: |
| 71 | + |
| 72 | +``` |
| 73 | +$id = $idcard->setId('130724197906126153')->getId(); |
| 74 | +``` |
| 75 | + |
| 76 | +### 检查身份证号码格式是否正确 |
| 77 | + |
| 78 | +``` |
| 79 | +$result = $idcard->check(); |
| 80 | +
|
| 81 | +$result = $idcard->setId('130724197906126153')->check(); |
| 82 | +``` |
| 83 | + |
| 84 | +### 通过身份证号获取生日 |
| 85 | + |
| 86 | +可以通过给getBirthday()方法传递一个分隔符参数来设置返回的生日格式,默认`-` |
| 87 | + |
| 88 | +``` |
| 89 | +$birthday = $idcard->getBirthday(); // 1979-06-12 |
| 90 | +$birthday = $idcard->getBirthday('.'); // 1979.06.12 |
| 91 | +$birthday = $idcard->getBirthday(''); // 19790612 |
| 92 | +``` |
| 93 | + |
| 94 | +### 通过身份证号获取年龄 |
| 95 | + |
| 96 | +``` |
| 97 | +$age = $idcard->getAge(); |
| 98 | +``` |
| 99 | + |
| 100 | +### 通过身份证号获取性别 |
| 101 | + |
| 102 | +可以通过给getGender()方法传递一个语言参数来设置返回的性格语言,默认中文 |
| 103 | + |
| 104 | +- $idcard::GENDER_CN 中文:男/女 |
| 105 | +- $idcard::GENDER_EN 英文:male/female |
| 106 | + |
| 107 | +``` |
| 108 | +$gender = $idcard->getGender(); // 男 |
| 109 | +$gender = $idcard->getGender($idcard::GENDER_EN); // male |
| 110 | +``` |
| 111 | + |
| 112 | +### 通过身份证号获取所在地区 |
| 113 | + |
| 114 | +可以通过给getRegion()方法传递一个分隔符来设置返回的地区信息格式,默认空格 |
| 115 | + |
| 116 | +``` |
| 117 | +$region = $idcard->getRegion(); // 河北省 张家口市 沽源县 |
| 118 | +$region = $idcard->getRegion(','); // 河北省,张家口市,沽源县 |
| 119 | +``` |
| 120 | + |
| 121 | +### 输出带*号的身份证号 |
| 122 | + |
| 123 | +有时候需要在输出身份证号的时候做一些字符处理,通过format()方法可以方便的转换成`前4位+N×*+后4位`格式 |
| 124 | + |
| 125 | +可以通过向format()方法传递一个分隔符参数来设置隐藏的数字替换符号,默认`*` |
| 126 | + |
| 127 | +``` |
| 128 | +$id = $idcard->format(); // 1307**********6153 |
| 129 | +$id = $idcard->format('-'); // 1307----------6153 |
| 130 | +``` |
| 131 | + |
| 132 | +可以通过向format()方法传递一个左位数参数来设置左侧需要保留的位数 |
| 133 | + |
| 134 | +``` |
| 135 | +$id = $idcard->format('*', 3); // 130***********6153 |
| 136 | +``` |
| 137 | + |
| 138 | +可以通过向format()方法传递一个右位数参数来设置右侧需要保留的位数 |
| 139 | + |
| 140 | +``` |
| 141 | +$id = $idcard->format('*', 3, 2); // 130*************53 |
| 142 | +``` |
| 143 | + |
| 144 | +需要注意的是,左位数和右位数之和如果跟身份证号码总长度一致,就是原样输出,如果大于号码长度,则返回false |
0 commit comments