PHP实现地址自动识别实例( 二 )

 
/** * 匹配正确的城市地址 * @param $address * @param $city_list * @param int $force * @param int $str_len * @return array **/function checkAddress($address, $city_list, $force=false, $str_len=2){ $num = 0; $list = array(); $result = array(); // 遍历所有可能存在的城市 foreach ($city_list as $city_key=>$city){ $city_name = mb_substr($city['region_name'], 0, $str_len,'utf-8'); // 判断是否存包含当前地址字符 $city_arr = explode($city_name, $address); // 如果存在相关字眼,保存该地址的所有子地址 if(count($city_arr) >= 2){ // 必须名称长度同时达到当前比对长度 if(strlen($city['region_name']) < $str_len){ continue; } $num ++; if(isset($city['child'])){ $list = $list + $city['child']; } $result[] = array( 'region_code' => $city_key, 'region_name' => $city['region_name'], 'list' =>$list, ); } } // 如果有多个存在,则加大字符匹配长度 if($num > 1 || $force){ $region_name1 = $result[0]['region_name']; $region_name2 = $result[1]['region_name']; if(strlen($region_name1) == strlen($region_name2) && strlen($region_name1) == $str_len){ $region_id1 = $result[0]['region_code']; $region_id2 = $result[1]['region_code']; $index = $region_id1 > $region_id2 ? 1 : 0; $result = $result[$index]; return $result; } return $this->checkAddress($address, $city_list, $force, $str_len+1); } else { $result[0]['list'] = $list; return $result[0]; }} 
/** * 根据原地址返回详细信息 * @param $address * @param $province * @param $city * @param $area * @return array **/function getAddressInfo($address, $province, $city, $district){ // 查找最后出现的地址 - 截取详细信息 if(!isset($province['region_name'])){ throw new Exception("请检查并正确填写省份(市辖区)"); } if(!isset($city['region_name'])){ throw new Exception("请检查并正确填写城市"); } if(!isset($district['region_name'])){ throw new Exception("请检查并正确填写区域(县/区/镇)"); } $find_str = ''; if($province['region_name']){ $find_str = $province['region_name']; if($city['region_name']){ $find_str = $city['region_name']; if(isset($district['region_name']) && $district['region_name']){ $find_str = $district['region_name']; } } } // 截取详细的信息 $find_str_len = mb_strlen($find_str,'utf-8'); for($i=0; $i<$find_str_len-1; $i++){ $substr = mb_substr($find_str,0,$find_str_len - $i, 'utf-8'); $end_index = mb_strpos($address, $substr); if ($end_index){ $address = mb_substr($address, $end_index + mb_strlen($substr) , mb_strlen($address) - $end_index); } } !empty($find_str) && $find_str = '|S*' . $find_str; $area['info'] = preg_replace("/s*|,|,|:|:{$find_str}/i", '', $address); if(empty($area['info'])){ throw new Exception("详细地址不存在,请检查"); } return $address = [ 'province' => $province['region_code'], 'city' => $city['region_code'], 'district' => $district['region_code'], 'info' => $area['info'] ];}前端html部分代码
基本上能看得懂的 。jquery用到 getDiscern();方法,手机号码,姓名,地址等input这里就不一一列出了 。大家根据下面的jquery都能想象到
<div class="form-group"> {!! Form::label('discern', '自动识别地址:',['class' => 'control-label col-sm-2']) !!} <div class="col-sm-5"> {!! Form::textarea('discern', '', ['class' => 'form-textarea form-control form-discern','rows' => 3]) !!} </div> <div class="col-sm-3" style="height: 75px;"> <button type="button" class="btn btn-info btn-sm discern" onclick="getDiscern();" >提交识别</button> <small class="ruleGet" style="color: #676a74;">*查看模板</small> </div></div> 
jquery代码部分
ajax post后交给url:getDiscern 处理,这个就是上面controller的方法,success返回的数据后再追加到每个input里,最后再清除掉自动识别地址框的数据
/** * 地址识别 * @returns {boolean} */function getDiscern(){ var discernDel = $(".form-discern").val(); if(!discernDel){ alert("请输入要识别的地址"); return false; } $.ajax({ type: 'POST', url: "{!! route('admin.user.getDiscern') !!}", data: { '_token': csrf_token(), 'discernDel': discernDel }, dataType: 'json', timeout: 50000, success: function (res) { if (res.code == 1) { $("input[name='addr[linkman]']").val(res.msg.name); $("input[name='user_name']").val(res.msg.mobile); $("input[name='addr[address]']").val(res.msg.info); //触发change事件 $('#province').val(res.msg.province).trigger('change'); $('#city').val(res.msg.city).trigger('change'); $('#area').val(res.msg.district).trigger('change'); //识别后清除 $(".form-discern").val(""); } else { alert(res.msg); } } })}


推荐阅读