userRepository = $userRepo; $this->userRepository = new AddressParse(); } public function getTest(Request $request){ // 解析包含用户信息的字符串 $string = $request->input('str');//'深圳市龙华区龙华街道1980科技文化产业园3栋317 张三 13800138000 518000 120113196808214821'; $r = Address::smart($string); return $r; print_r($r); // 解析不包含用户信息的字符串 $string = '吉林省长春市朝阳区开运街领秀朝阳小区333弄8号502'; $r = Address::smart($string, $user = false); print_r($r); // 测试 // $test = array( // '北京市东城区宵云路36号国航大厦一层', // '甘肃省东乡族自治县布楞沟村1号', // '成都市双流区宵云路36号国航大厦一层', // '内蒙古自治区乌兰察布市公安局交警支队车管所', // '长春市朝阳区宵云路36号国航大厦一层', // '成都市高新区天府软件园B区科技大楼', // '双流区郑通路社保局区52050号', // '岳市岳阳楼区南湖求索路碧灏花园A座1101', // '四川省 凉山州美姑县东方网肖小区18号院', // '重庆攀枝花市东区机场路3中学校', // '渝北区渝北中学51200街道地址', // '13566892356天津天津市红桥区水木天成1区临湾路9-3-1101', // '苏州市昆山市青阳北路时代名苑20号311室', // '崇州市崇阳镇金鸡万人小区兴盛路105-107', // '四平市双辽市辽北街道', // '梧州市奥奇丽路10-9号A幢地层(礼迅贸易有限公司)卢丽丽', // '江西省抚州市东乡区孝岗镇恒安东路125号1栋3单元502室 13511112222 吴刚', // '清远市清城区石角镇美林湖大东路口佰仹公司 郑万顺 15345785872', // ); // // foreach ($test as $key => $v) { // $r = Address::smart($v, $user = false); // print_r($r); // } } public static function smart_parse(Request $request) { //解析结果 $parse = []; $address=$request->input('discernDel'); //1. 过滤掉收货地址中的常用说明字符,排除干扰词 $search = ['收货地址', '地址', '收货人', '收件人', '收货', '邮编', '电话', '身份证号码', '身份证号', '身份证', ':', ':', ';', ';', ',', ',', '。', ]; $replace = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']; $address = str_replace($search, $replace, $address); //2. 把空白字符(包括空格\r\n\t)都换成一个空格 $address = preg_replace('/\s{1,}/', ' ', $address); //3. 去除手机号码中的短横线 如136-3333-6666 主要针对苹果手机 $address = preg_replace('/0-|0?(\d{3})-(\d{4})-(\d{4})/', '$1$2$3', $address); //4. 提取中国境内身份证号码 preg_match('/\d{18}|\d{17}X/i', $address, $match); if ($match && $match[0]) { $parse['idno'] = strtoupper($match[0]); $address = str_replace($match[0], '', $address); } //5. 提取11位手机号码或者7位以上座机号 preg_match('/\d{7,11}|\d{3,4}-\d{6,8}/', $address, $match); if ($match && $match[0]) { $parse['mobile'] = $match[0]; $address = str_replace($match[0], '', $address); } //6. 提取6位邮编 邮编也可用后面解析出的省市区地址从数据库匹配出 preg_match('/\d{6}/', $address, $match); if ($match && $match[0]) { $parse['postcode'] = $match[0]; $address = str_replace($match[0], '', $address); } //再次把2个及其以上的空格合并成一个,并TRIM $address = trim(preg_replace('/ {2,}/', ' ', $address)); //按照空格切分 长度长的为地址 短的为姓名 因为不是基于自然语言分析,所以采取统计学上高概率的方案 $split_arr = explode(' ', $address); if (count($split_arr) > 1) { $parse['name'] = $split_arr[0]; foreach ($split_arr as $value) { if (strlen($value) < strlen($parse['name'])) { $parse['name'] = $value; } } $address = trim(str_replace($parse['name'], '', $address)); } $address_detail = DetailParse::detail_parse($address); $parse['address_detail'] = $address_detail; $parse['detail'] = $address; //parse['detail']详细地址可以传入另一个文件的函数,用来解析出:省,市,区,街道地址 return $parse; } /** * Function:地址识别 * Author:cyw0413 * @param Request $request * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response| * \Laravel\Lumen\Http\ResponseFactory|\Symfony\Component\HttpFoundation\Response */ public function getDiscern(Request $request) { $discernDel = $request->input('discernDel'); return $this->userRepository->getAddress($discernDel); try{ DB::beginTransaction(); $address = $this->userRepository->getDiscern($discernDel); DB::commit(); }catch (\Exception $e){ DB::rollBack(); $msg = "信息提示:".$e->getMessage().",行:".$e->getLine(); return response(['code' => 0, 'msg'=>$msg]); } return response(['code'=>1,'msg'=>$address]); //地址识别完成 } public function DealAddress(Request $request){ $data=[]; $details=$request->input('details'); $r = Address::smart($details); return $this->success([ 'phone'=>array_key_exists('mobile',$r)?$r['mobile']:'', 'name'=>array_key_exists('name',$r)?$r['name']:'', 'province'=>$r['province'], 'city'=>$r['city'], 'area'=>$r['region'], 'street'=>$r['street'], ]); preg_match_all("/1((34[0-8]\d{7})|((3[0-3|5-9])|(4[5-7|9])|(5[0-3|5-9])|(66)|(7[2-3|5-8])|(8[0-9])|(9[1|8|9]))\d{8})/", $details,$phones); if($phones){ if($phones[0]){ $data['phone']=$phones[0][0]; } } $name=trim(mb_substr($details,'0',mb_strpos($details,$phones[0][0]))); $regex = "/\ |\/|\~|\!|\@|\#|\\$|\%|\^|\&|\*|\(|\)|\_|\+|\{|\}|\:|\<|\>|\?|\[|\]|\,|\.|\/|\;|\,|\。|\。|\“|\‘|\?|\!|\【|\】|\;|\:|\¥|\(|\)|\'|\`|\-|\=|\\\|\|/"; $data['name'] = preg_replace($regex,"",$name); $client = new Client(); $url = 'https://restapi.amap.com/v5/place/text?parameters'; $array = [ 'query' => [ 'keywords' => $details, 'output' => 'json', 'key' => '4e0ce153265426f2d2ceef069e5eb552', ] ]; $response = $client->request('GET', $url, $array); $ad = json_decode($response->getBody()->getContents()); if ($ad->pois){ $data['province']=$ad->pois[0]->pname; $data['city']=$ad->pois[0]->cityname; $data['area']=$ad->pois[0]->adname; } return $this->success($data); } }