-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringParser.php
More file actions
executable file
·274 lines (227 loc) · 7.93 KB
/
StringParser.php
File metadata and controls
executable file
·274 lines (227 loc) · 7.93 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
/*
* Copyright (c) 2023. Ankio. All Rights Reserved.
*/
namespace library\ip;
class StringParser
{
/**
* 运营商词典
*
* @var array
*/
private static $dictIsp = [
'联通',
'移动',
'铁通',
'电信',
'长城',
'聚友',
];
/**
* 中国直辖市
*
* @var array
*/
private static $dictCityDirectly = [
'北京',
'天津',
'重庆',
'上海',
];
private static $dictDistrictBlackTails = [
'校区',
'学区',
];
/**
* 中国省份
*
* @var array
*/
private static $dictProvince = [
'北京',
'天津',
'重庆',
'上海',
'河北',
'山西',
'辽宁',
'吉林',
'黑龙江',
'江苏',
'浙江',
'安徽',
'福建',
'江西',
'山东',
'河南',
'湖北',
'湖南',
'广东',
'海南',
'四川',
'贵州',
'云南',
'陕西',
'甘肃',
'青海',
'台湾',
'内蒙古',
'广西',
'宁夏',
'新疆',
'西藏',
'香港',
'澳门',
];
/**
* $location = [
* 'country', 'area', 'ip'
* ];
*
*
* @param $location
* @param bool $withOriginal debug 用,是否返回原始数据
* @return array
*/
public static function parse($location, $withOriginal = false): array
{
$org = $location;
$result = [];
$isChina = false;
$separatorProvince = '省';
$separatorCity = '市';
$separatorCounty = '县';
$separatorDistrict = '区';
if (!$location) {
$result['error'] = 'file open failed';
return $result;
}
//ipv6 会包含中国 故意去掉
if (strpos($location['country'], "中国") === 0) {
$location['country'] = str_replace("中国", "", $location['country']);
}
$location['org_country'] = $location['country']; //北京市朝阳区
$location['org_area'] = $location['area']; // 金桥国际小区
$location['province'] = $location['city'] = $location['county'] = '';
$_tmp_province = explode($separatorProvince, $location['country']);
//存在 省 标志 xxx省yyyy 中的yyyy
if (isset($_tmp_province[1])) {
$isChina = true;
//省
$location['province'] = $_tmp_province[0]; //河北
if (strpos($_tmp_province[1], $separatorCity) !== false) {
$_tmp_city = explode($separatorCity, $_tmp_province[1]);
//市
$location['city'] = $_tmp_city[0] . $separatorCity;
//县
if (isset($_tmp_city[1])) {
if (strpos($_tmp_city[1], $separatorCounty) !== false) {
$_tmp_county = explode($separatorCounty, $_tmp_city[1]);
$location['county'] = $_tmp_county[0] . $separatorCounty;
}
//区
if (!$location['county'] && strpos($_tmp_city[1], $separatorDistrict) !== false) {
$_tmp_qu = explode($separatorDistrict, $_tmp_city[1]);
$location['county'] = $_tmp_qu[0] . $separatorDistrict;
}
}
}
} else {
//处理内蒙古不带省份类型的和直辖市
foreach (self::$dictProvince as $key => $value) {
if (false !== strpos($location['country'], $value)) {
$isChina = true;
$location['province'] = $value;
//直辖市
if (in_array($value, self::$dictCityDirectly)) {
//直辖市
$_tmp_province = explode($value, $location['country']);
//市辖区
if (isset($_tmp_province[1])) {
$_tmp_province[1] = self::lTrim($_tmp_province[1], $separatorCity);
if (strpos($_tmp_province[1], $separatorDistrict) !== false) {
$_tmp_qu = explode($separatorDistrict, $_tmp_province[1]);
//解决 休息休息校区 变成城市区域
$isHitBlackTail = false;
foreach (self::$dictDistrictBlackTails as $blackTail) {
//尾
if (mb_substr($_tmp_qu[0], -mb_strlen($blackTail)) == $blackTail) {
$isHitBlackTail = true;
break;
}
}
//校区,学区
if ((!$isHitBlackTail) && mb_strlen($_tmp_qu[0]) < 5) {
//有点尴尬
$location['city'] = $_tmp_qu[0] . $separatorDistrict;
}
}
}
} else {
//没有省份标志 只能替换
$_tmp_city = str_replace($location['province'], '', $location['country']);
//防止直辖市捣乱 上海市xxx区 =》 市xx区
$_tmp_city = self::lTrim($_tmp_city, $separatorCity);
//内蒙古 类型的 获取市县信息
if (strpos($_tmp_city, $separatorCity) !== false) {
//市
$_tmp_city = explode($separatorCity, $_tmp_city);
$location['city'] = $_tmp_city[0] . $separatorCity;
//县
if (isset($_tmp_city[1])) {
if (strpos($_tmp_city[1], $separatorCounty) !== false) {
$_tmp_county = explode($separatorCounty, $_tmp_city[1]);
$location['county'] = $_tmp_county[0] . $separatorCounty;
}
//区
if (!$location['county'] && strpos($_tmp_city[1], $separatorDistrict) !== false) {
$_tmp_qu = explode($separatorDistrict, $_tmp_city[1]);
$location['county'] = $_tmp_qu[0] . $separatorDistrict;
}
}
}
}
break;
}
}
}
if ($isChina) {
$location['country'] = '中国';
}
$result['ip'] = $location['ip'];
$result['country'] = $location['country'];
$result['province'] = $location['province'];
$result['city'] = $location['city'];
$result['county'] = $location['county'];
$result['area'] = $location['country'] . $location['province'] . $location['city'] . $location['county'] . ' ' . $location['org_area'];
$result['isp'] = self::getIsp($result['area']);
if ($withOriginal) {
$result['org'] = $org;
}
return $result;
}
private static function lTrim($word, $w)
{
$pos = mb_stripos($word, $w);
if ($pos === 0) {
$word = mb_substr($word, 1);
}
return $word;
}
/**
* @param $str
* @return string
*/
private static function getIsp($str)
{
$ret = '';
foreach (self::$dictIsp as $k => $v) {
if (str_contains($str, $v)) {
$ret = $v;
break;
}
}
return $ret;
}
}