|
@@ -162,5 +162,48 @@ class areaModel extends Model {
|
|
|
return $data;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 格式化地址, 返回格式化字符串, 省\t市\t区
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public function formatAddress($address) {
|
|
|
+ $topLevelAreas = $this->getTopLevelAreas();
|
|
|
+ $cityProvince = $this->getCityProvince();
|
|
|
+ $areaNames = $this->getAreaNames();
|
|
|
+
|
|
|
+ // 过滤空格等各种不可见字符
|
|
|
+ $address = trim($address);// 首先去掉头尾空格
|
|
|
+ $address = preg_replace('/\s(?=\s)/', '', $address);// 接着去掉两个空格以上的
|
|
|
+ $address = preg_replace('/[\n\r\t]/', '', $address);// 最后将非空格替换为没空格
|
|
|
+
|
|
|
+ // 省
|
|
|
+ foreach ($topLevelAreas as $key => $topValue) {
|
|
|
+ $topLevelLen = strlen($topValue);
|
|
|
+
|
|
|
+ // 匹配不到省, 跳过
|
|
|
+ $ret = strncmp($address, $topValue, $topLevelLen);
|
|
|
+ if ($ret != 0) continue;
|
|
|
+
|
|
|
+ $topLevelLen = mb_strlen($topValue);
|
|
|
+ // 市
|
|
|
+ foreach ($cityProvince as $cityValue => $parent) {
|
|
|
+ if ($parent != $key) continue;
|
|
|
+
|
|
|
+ $cityValue = $areaNames[$cityValue];
|
|
|
+ $cityLen = mb_strlen($cityValue);
|
|
|
+
|
|
|
+ $tmp = mb_substr($address, $topLevelLen, $cityLen);
|
|
|
+ // 匹配不到省, 跳过
|
|
|
+ if (strcmp($tmp, $cityValue) != 0) continue;
|
|
|
+
|
|
|
+ // 地区
|
|
|
+ $tmp = mb_substr($address, $topLevelLen+$cityLen, NULL);
|
|
|
+ return "{$topValue}\t{$cityValue}\t{$tmp}";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
protected $cachedData;
|
|
|
}
|