只要满足 省市区 格式的字符串,都能正确拆分
function splitRegion($region)
{
// 定义匹配省、市、县的模式,注意处理*自治*与直接接地名的情况
$provincePattern = '/(.*?(?:省|自治区|直辖市|特别行政区))/u';
$cityPattern = '/(.*?(?:市|自治州))/u';
$countyPattern = '/(.*?(?:区|县|市|旗|自治县|自治旗))/u';
$province = '';
$city = '';
$county = '';
if(preg_match($provincePattern, $region, $matches)){
$province = $matches[1];
$region = str_replace($province, '', $region);
}
if(preg_match($cityPattern, $region, $matches)){
$city = $matches[1];
$region = str_replace($city, '', $region);
}
$county = trim($region); //为省份和城市之后的所有字符
return array(
'province' => trim($province),
'city' => trim($city),
'district' => trim($county)
);
}
Comments NOTHING