-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIs.php
198 lines (181 loc) · 5.74 KB
/
Is.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
namespace Yonna\Foundation;
use DateTime;
use Exception;
class Is
{
/**
* 验证手机号码
* @param $mobile
* @return bool
*/
public static function mobile($mobile)
{
$subMob = substr($mobile, 0, 2);
$telList = array(
'00', '11', '12', '13', '14', '15', '16', '17', '18', '19',
);
$bool1 = preg_match('/^(\d{11}|\d{8})$/', $mobile) ? true : false;
$bool2 = (in_array($subMob, $telList)) ? true : false;
return ($bool1 && $bool2);
}
/**
* 验证email格式
* @param $email
* @return bool
*/
public static function email($email)
{
return preg_match('/^[\w\.\-]+@[\w\-]+(\.[a-z]+){1,2}$/i', $email) ? true : false;
}
/**
* 验证身份证NO
* @param $idCardNo
* @param bool $isStrict
* @return bool
*/
public static function identityCardNo($idCardNo, $isStrict = false)
{
/**
* 计算身份证校验码,根据国家标准GB 11643-1999
* @param $idCardBase
* @return bool|mixed
*/
function getVerifyBit($idCardBase)
{
if (strlen($idCardBase) != 17) {
return false;
}
//加权因子
$factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
//校验码对应值
$verify_number_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
$checksum = 0;
for ($i = 0; $i < strlen($idCardBase); $i++) {
$checksum = $checksum + round(substr($idCardBase, $i, 1), 3) * $factor[$i];
}
$mod = $checksum % 11;
$verify_number = $verify_number_list[$mod];
return $verify_number;
}
$idCardLength = strlen($idCardNo);
//长度验证
if (!preg_match('/^\d{17}(\d|x)$/i', $idCardNo) and !preg_match('/^\d{15}$/i', $idCardNo)) {
return false;
}
if ($isStrict) {
//地区验证
$city = 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 (!array_key_exists(intval(substr($idCardNo, 0, 2)), $city)) {
return false;
}
//15位身份证验证生日,转换为18位
if ($idCardLength == 15) {
$sBirthday = '19' . substr($idCardNo, 6, 2) . '-' . substr($idCardNo, 8, 2) . '-' . substr($idCardNo, 10, 2);
try {
$d = new DateTime($sBirthday);
$dd = $d->format('Y-m-d');
if ($sBirthday != $dd) return false;
} catch (Exception $e) {
}
$idCardNo = substr($idCardNo, 0, 6) . "19" . substr($idCardNo, 6, 9);//15to18
$Bit18 = getVerifyBit($idCardNo);//算出第18位校验码
$idCardNo = $idCardNo . $Bit18;
}
//18位身份证处理
$sBirthday = substr($idCardNo, 6, 4) . '-' . substr($idCardNo, 10, 2) . '-' . substr($idCardNo, 12, 2);
try {
$d = new DateTime($sBirthday);
$dd = $d->format('Y-m-d');
if ($sBirthday != $dd) return false;
} catch (Exception $e) {
}
//身份证编码规范验证
$idCardNo_base = substr($idCardNo, 0, 17);
if (strtoupper(substr($idCardNo, 17, 1)) != getVerifyBit($idCardNo_base)) {
return false;
}
}
return true;
}
/**
* 验证微信OPENID格式
* @param $open_id
* @return bool
*/
public static function wechatOpenId($open_id)
{
return (strlen($open_id) > 15) ? true : false;
}
/**
* 验证IP格式
* @param $str
* @return bool|int
*/
public static function ip($str)
{
$ip = explode('.', $str);
for ($i = 0; $i < count($ip); $i++) {
if ($ip[$i] > 255) {
return false;
}
}
return preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $str);
}
/**
* 是否 windows
* @return bool
*/
public static function windows()
{
return strstr(PHP_OS, 'WIN') ? true : false;
}
/**
* 是否 unix
* @return bool
*/
public static function unix()
{
return strstr(PHP_OS, 'unix') ? true : false;
}
/**
* 是否 linux
* @return bool
*/
public static function linux()
{
return strstr(PHP_OS, 'Linux') ? true : false;
}
/**
* 是否 mac
* @return bool
*/
public static function mac()
{
return strstr(PHP_OS, 'macintosh') ? true : false;
}
/**
* 是否 AJAX
* @return bool
*/
public static function ajax()
{
return ($_SERVER['HTTP_X_REQUESTED_WITH'] ?? null) === 'XMLHttpRequest';
}
/**
* 是否CGI
* @return bool
*/
public static function cgi()
{
return (0 === strpos(PHP_SAPI, 'cgi') || false !== strpos(PHP_SAPI, 'fcgi')) ? true : false;
}
/**
* 是否CLI
* @return bool
*/
public static function cli()
{
return PHP_SAPI == 'cli' ? true : false;
}
}