php 智能识别收货地址

public function apiAnalysisPublic($analysis){

        $str = $analysis;
        $patt = '/1[2345678]\d{9}/';
        preg_match  ($patt,$str,$phone);
        if(empty($phone)){
            return 101;
        }
        $name = substr($str,0,strrpos($str,$phone[0]));
        if(empty($name)){
            return 101;
        }
        $address = substr($str,strripos($str,$phone[0])+11);
        $address_all = $address;
        if(empty($address_all)){
            return 101;
        }

//        $address = "马云13511111111河北省石家庄市桥西区富康路姚家园3楼0";
        preg_match('/(.*?(省|自治区|北京市|天津市))/', $address, $matches);
        if (count($matches) > 1) {
            $province = $matches[count($matches) - 2];
            $address = str_replace($province, '', $address);
        }
        preg_match('/(.*?(市|自治州|地区|区划|县))/', $address, $matches);
        if (count($matches) > 1) {
            $city = $matches[count($matches) - 2];
            $address = str_replace($city, '', $address);
        }
        preg_match('/(.*?(区|县|镇|乡|街道))/', $address, $matches);
        if (count($matches) > 1) {
            $area = $matches[count($matches) - 2];
            $address = str_replace($area, '', $address);
        }
        $conurbation = [
            'province' => isset($province) ? trim($province) : '',
            'city' => isset($city) ? trim($city) : '',
            'area' => isset($area) ? trim($area) : '',
        ];
        if(empty($conurbation['province'])){
            $conurbation['province'] = $conurbation['city'];
        }
        if(empty($conurbation['city'])){
            $conurbation['city'] = $conurbation['province'];
        }
        if(empty($conurbation['province']) || empty($conurbation['city']) || empty($conurbation['area'])){
            return 101;
        }
        /*查找自己数据库中的省id*/
        $chinaProvinceModel = new ChinaProvince;
        $province_id = $chinaProvinceModel->findOne(['name'=>['like','%'.$conurbation['province'].'%']],'id');
        /*查找自己数据库中市id*/
        $chinaCityModel = new ChinaCity;
        $city_id = $chinaCityModel->findOne(['name'=>['like','%'.$conurbation['city'].'%']],'id');
        /*查找自己数据库中县区id*/
        $chinaCityModel = new ChinaArea;
        $area_id = $chinaCityModel->findOne(['name'=>['like','%'.$conurbation['area'].'%']],'id');

        if(empty($province_id) || empty($city_id) || empty($area_id)){
            return 101;
        }

        $data = [
            'province_id'=>$province_id,
            'city_id'=>$city_id,
            'area_id'=>$area_id,
            'province'=>$conurbation['province'],
            'city'=>$conurbation['city'],
            'area'=>$conurbation['area'],
            'name'=>trim($name),
            'phone'=>trim($phone[0]),
            'address'=>trim($address_all),
        ];
        return $data;
    }

 


版权声明:本文为qq_30202073原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。