Address.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. namespace App\Handlers;
  3. class Address extends Arr{
  4. /*
  5. ** 智能解析
  6. */
  7. public static function smart($string, $user = true)
  8. {
  9. if ($user) {
  10. $decompose = self::decompose($string);
  11. $re = $decompose;
  12. } else {
  13. $re['addr'] = $string;
  14. }
  15. $fuzz = self::fuzz($re['addr']);
  16. $parse = self::parse($fuzz['a1'], $fuzz['a2'], $fuzz['a3']);
  17. $re['province'] = array_key_exists('province',$parse)?$parse['province']:'';
  18. $re['city'] = array_key_exists('city',$parse)?$parse['city']:'';
  19. $re['region'] = array_key_exists('region',$parse)?$parse['region']:'';
  20. $re['street'] = ($fuzz['street']) ? $fuzz['street'] : '';
  21. $re['street'] = str_replace([$re['region'], $re['city'], $re['province']], ['', '', ''], $re['street']);
  22. return $re;
  23. }
  24. /*
  25. ** 分离手机号(座机),身份证号,姓名等用户信息
  26. */
  27. public static function decompose($string)
  28. {
  29. $compose = array();
  30. $search = array('收货地址', '详细地址', '地址', '收货人', '收件人', '收货', '所在地区', '邮编', '电话', '手机号码','身份证号码', '身份证号', '身份证', ':', ':', ';', ';', ',', ',', '。');
  31. $replace = array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
  32. $string = str_replace($search, $replace, $string);
  33. $string = preg_replace('/\s{1,}/', ' ', $string);
  34. $string = preg_replace('/0-|0?(\d{3})-(\d{4})-(\d{4})/', '$1$2$3', $string);
  35. preg_match('/\d{18}|\d{17}X/i', $string, $match);
  36. if ($match && $match[0]) {
  37. $compose['idn'] = strtoupper($match[0]);
  38. $string = str_replace($match[0], '', $string);
  39. }
  40. preg_match('/\d{7,11}|\d{3,4}-\d{6,8}/', $string, $match);
  41. if ($match && $match[0]) {
  42. $compose['mobile'] = $match[0];
  43. $string = str_replace($match[0], '', $string);
  44. }
  45. preg_match('/\d{6}/', $string, $match);
  46. if ($match && $match[0]) {
  47. $compose['postcode'] = $match[0];
  48. $string = str_replace($match[0], '', $string);
  49. }
  50. $string = trim(preg_replace('/ {2,}/', ' ', $string));
  51. $split_arr = explode(' ', $string);
  52. if (count($split_arr) > 1) {
  53. $compose['name'] = $split_arr[0];
  54. foreach ($split_arr as $value) {
  55. if (strlen($value) < strlen($compose['name'])) {
  56. $compose['name'] = $value;
  57. }
  58. }
  59. $string = trim(str_replace($compose['name'], '', $string));
  60. }
  61. $compose['addr'] = $string;
  62. return $compose;
  63. }
  64. /*
  65. ** 根据统计规律分析出二三级地址
  66. */
  67. public static function fuzz($addr)
  68. {
  69. $addr_origin = $addr;
  70. $addr = str_replace([' ', ','], ['', ''], $addr);
  71. $addr = str_replace('自治区', '省', $addr);
  72. $addr = str_replace('自治州', '州', $addr);
  73. $addr = str_replace('小区', '', $addr);
  74. $addr = str_replace('校区', '', $addr);
  75. $a1 = '';
  76. $a2 = '';
  77. $a3 = '';
  78. $street = '';
  79. if (mb_strpos($addr, '县') !== false && mb_strpos($addr, '县') < floor((mb_strlen($addr) / 3) * 2) || (mb_strpos($addr, '区') !== false && mb_strpos($addr, '区') < floor((mb_strlen($addr) / 3) * 2)) || mb_strpos($addr, '旗') !== false && mb_strpos($addr, '旗') < floor((mb_strlen($addr) / 3) * 2)) {
  80. if (mb_strstr($addr, '旗')) {
  81. $deep3_keyword_pos = mb_strpos($addr, '旗');
  82. $a3 = mb_substr($addr, $deep3_keyword_pos - 1, 2);
  83. }
  84. if (mb_strstr($addr, '区')) {
  85. $deep3_keyword_pos = mb_strpos($addr, '区');
  86. if (mb_strstr($addr, '市')) {
  87. $city_pos = mb_strpos($addr, '市');
  88. $zone_pos = mb_strpos($addr, '区');
  89. $a3 = mb_substr($addr, $city_pos + 1, $zone_pos - $city_pos);
  90. } else {
  91. $a3 = mb_substr($addr, $deep3_keyword_pos - 2, 3);
  92. }
  93. }
  94. if (mb_strstr($addr, '县')) {
  95. $deep3_keyword_pos = mb_strpos($addr, '县');
  96. if (mb_strstr($addr, '市')) {
  97. $city_pos = mb_strpos($addr, '市');
  98. $zone_pos = mb_strpos($addr, '县');
  99. $a3 = mb_substr($addr, $city_pos + 1, $zone_pos - $city_pos);
  100. } else {
  101. if (mb_strstr($addr, '自治县')) {
  102. $a3 = mb_substr($addr, $deep3_keyword_pos - 6, 7);
  103. if (in_array(mb_substr($a3, 0, 1), ['省', '市', '州'])) {
  104. $a3 = mb_substr($a3, 1);
  105. }
  106. } else {
  107. $a3 = mb_substr($addr, $deep3_keyword_pos - 2, 3);
  108. }
  109. }
  110. }
  111. $street = mb_substr($addr_origin, $deep3_keyword_pos + 1);
  112. } else {
  113. if (mb_strripos($addr, '市')) {
  114. if (mb_substr_count($addr, '市') == 1) {
  115. $deep3_keyword_pos = mb_strripos($addr, '市');
  116. $a3 = mb_substr($addr, $deep3_keyword_pos - 2, 3);
  117. $street = mb_substr($addr_origin, $deep3_keyword_pos + 1);
  118. } else if (mb_substr_count($addr, '市') >= 2) {
  119. $deep3_keyword_pos = mb_strripos($addr, '市');
  120. $a3 = mb_substr($addr, $deep3_keyword_pos - 2, 3);
  121. $street = mb_substr($addr_origin, $deep3_keyword_pos + 1);
  122. }
  123. } else {
  124. $a3 = '';
  125. $street = $addr;
  126. }
  127. }
  128. if (mb_strpos($addr, '市') || mb_strstr($addr, '盟') || mb_strstr($addr, '州')) {
  129. if ($tmp_pos = mb_strpos($addr, '市')) {
  130. $a2 = mb_substr($addr, $tmp_pos - 2, 3);
  131. } else if ($tmp_pos = mb_strpos($addr, '盟')) {
  132. $a2 = mb_substr($addr, $tmp_pos - 2, 3);
  133. } else if ($tmp_pos = mb_strpos($addr, '州')) {
  134. if ($tmp_pos = mb_strpos($addr, '自治州')) {
  135. $a2 = mb_substr($addr, $tmp_pos - 4, 5);
  136. } else {
  137. $a2 = mb_substr($addr, $tmp_pos - 2, 3);
  138. }
  139. }
  140. } else {
  141. $a2 = '';
  142. }
  143. $a2;
  144. $r = array(
  145. 'a1' => $a1,
  146. 'a2' => $a2,
  147. 'a3' => $a3,
  148. 'street' => $street,
  149. );
  150. return $r;
  151. }
  152. /*
  153. ** 智能解析出省市区+街道地址
  154. */
  155. public static function parse($a1, $a2, $a3)
  156. {
  157. $a1_data=parent::getArr()['data1'];
  158. $a2_data=parent::getArr()['data2'];
  159. $a3_data=parent::getArr()['data3'];
  160. $r = array();
  161. if ($a3 != '') {
  162. $area3_matches = array();
  163. foreach ($a3_data as $id => $v) {
  164. if (mb_strpos($v['name'], $a3) !== false) {
  165. $area3_matches[$id] = $v;
  166. }
  167. }
  168. if ($area3_matches && count($area3_matches) > 1) {
  169. if ($a2) {
  170. foreach ($a2_data as $id => $v) {
  171. if (mb_strpos($v['name'], $a2) !== false) {
  172. $area2_matches[$id] = $v;
  173. }
  174. }
  175. if ($area2_matches) {
  176. foreach ($area3_matches as $id => $v) {
  177. if (isset($area2_matches[$v['pid']])) {
  178. $r['city'] = $area2_matches[$v['pid']]['name'];
  179. $r['region'] = $v['name'];
  180. $sheng_id = $area2_matches[$v['pid']]['pid'];
  181. $r['province'] = $a1_data[$sheng_id]['name'];
  182. }
  183. }
  184. }
  185. } else {
  186. $r['province'] = '';
  187. $r['city'] = '';
  188. $r['region'] = $a3;
  189. }
  190. } else if ($area3_matches && count($area3_matches) == 1) {
  191. foreach ($area3_matches as $id => $v) {
  192. $city_id = $v['pid'];
  193. $r['region'] = $v['name'];
  194. }
  195. $city = $a2_data[$city_id];
  196. $province = $a1_data[$city['pid']];
  197. $r['province'] = $province['name'];
  198. $r['city'] = $city['name'];
  199. } else if (empty($area3_matches) && $a2 == $a3) {
  200. foreach ($a2_data as $id => $v) {
  201. if (mb_strpos($v['name'], $a2) !== false) {
  202. $area2_matches[$id] = $v;
  203. $sheng_id = $v['pid'];
  204. $r['city'] = $v['name'];
  205. }
  206. }
  207. $r['province'] = $a1_data[$sheng_id]['name'];
  208. $r['region'] = '';
  209. }
  210. }
  211. return $r;
  212. }
  213. }