php获取地址中的省市区以及详细地址
实例
<?php function get_address($address){ preg_match('/(.*?(省|自治区|北京市|天津市|上海市|重庆市|澳门特别行政区|香港特别行政区))/', $address, $matches); if (count($matches) > 1) { $province = $matches[count($matches) - 2]; $address = preg_replace('/(.*?(省|自治区|北京市|天津市|上海市|重庆市|澳门特别行政区|香港特别行政区))/','', $address, 1); } 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); } return [ 'province' => isset($province) ? $province : '', 'city' => isset($city) ? $city : '', 'district' => isset($area) ? $area : '', "address" => $address ]; } print_r(get_address('广东省深圳市南山区滨海大道')); ?>
运行实例 »
点击 "运行实例" 按钮查看在线实例