1 | <?php |
||
2 | /** |
||
3 | * Class Regular |
||
4 | * |
||
5 | * @link https://www.icy2003.com/ |
||
6 | * @author icy2003 <[email protected]> |
||
7 | * @copyright Copyright (c) 2017, icy2003 |
||
8 | */ |
||
9 | |||
10 | namespace icy2003\php\ihelpers; |
||
11 | |||
12 | /** |
||
13 | * 正则相关 |
||
14 | * |
||
15 | * 常用的正则表达式校验 |
||
16 | */ |
||
17 | class Regular |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * 验证邮箱 |
||
22 | * |
||
23 | * @see http://www.regular-expressions.info/email.html |
||
24 | * |
||
25 | * @param string $email 邮箱 |
||
26 | * |
||
27 | * @return boolean |
||
28 | */ |
||
29 | 1 | public static function email($email) |
|
30 | { |
||
31 | 1 | return (bool)preg_match('/^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/', $email); |
|
32 | } |
||
33 | |||
34 | /** |
||
35 | * 验证 IP |
||
36 | * |
||
37 | * @see https://www.regular-expressions.info/ip.html |
||
38 | * |
||
39 | * @param string $ip IP |
||
40 | * |
||
41 | * @return boolean |
||
42 | */ |
||
43 | 1 | public static function ip($ip) |
|
44 | { |
||
45 | 1 | return (bool)preg_match('/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/', $ip); |
|
46 | } |
||
47 | |||
48 | /** |
||
49 | * 验证手机 |
||
50 | * |
||
51 | * @param string $mobile 手机号 |
||
52 | * |
||
53 | * @return boolean |
||
54 | */ |
||
55 | 1 | public static function mobile($mobile) |
|
56 | { |
||
57 | 1 | return (bool)preg_match('/^1[3-9][0-9]\d{8}$/', $mobile); |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * 验证身份证号 |
||
62 | * |
||
63 | * @param string $idCard 身份证号 |
||
64 | * |
||
65 | * @return boolean |
||
66 | */ |
||
67 | 1 | public static function idCard($idCard) |
|
68 | { |
||
69 | 1 | return (bool)preg_match('/(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/', $idCard); |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * 验证 URL |
||
74 | * |
||
75 | * @param string $url |
||
76 | * |
||
77 | * @return boolean |
||
78 | */ |
||
79 | 1 | public static function url($url) |
|
80 | { |
||
81 | 1 | return (bool)preg_match('/^https?:\/\//', $url); |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * 判断是否是中文 |
||
86 | * |
||
87 | * @param string $chinese 中文字符 |
||
88 | * |
||
89 | * @return boolean |
||
90 | */ |
||
91 | 1 | public static function chinese($chinese) |
|
92 | { |
||
93 | 1 | $chinese = Charset::toUtf($chinese); |
|
94 | 1 | return (bool)preg_match('/[\x{4e00}-\x{9fa5}]+/u', $chinese); |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * 关闭 JIT |
||
99 | * |
||
100 | * @param boolean $isOn 是否开启,默认 false |
||
101 | * |
||
102 | * @return void |
||
103 | */ |
||
104 | 1 | public static function jitOff($isOn = false) |
|
105 | { |
||
106 | /** |
||
107 | * @see /samples/php7preg_bug.php |
||
108 | */ |
||
109 | 1 | ini_set('pcre.jit', true === $isOn ? 1 : 0); |
|
110 | 1 | } |
|
111 | |||
112 | /** |
||
113 | * 判断一个正则是否合法 |
||
114 | * |
||
115 | * @param string $pattern 正则表达式 |
||
116 | * |
||
117 | * @return boolean |
||
118 | */ |
||
119 | 1 | public static function isLegal($pattern) |
|
120 | { |
||
121 | 1 | return false !== @preg_match($pattern, null); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
122 | } |
||
123 | } |
||
124 |