Skip to content

Instantly share code, notes, and snippets.

@billy3321
Last active October 22, 2016 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billy3321/153935b6c6361194f6a2258e375572c3 to your computer and use it in GitHub Desktop.
Save billy3321/153935b6c6361194f6a2258e375572c3 to your computer and use it in GitHub Desktop.
把完整地號分開來
<?php
// This code release under MIT License.
$test_array = Array("宜蘭縣冬山鄉興平段0444號",
"花蓮縣花蓮市富國段0419號",
"嘉義市東區長竹段0908-0003號",
"新竹市新竹市區民富段2715號",
"臺南市永康區二王段0029號",
"臺北市大安區復興段三小段0645-0003號",
"基隆市中正區調和段0376-0003號");
function clean_lot_number($lot_number) {
$lot_number = str_replace(" ","", $lot_number);
$lot_number = str_replace("台","臺", $lot_number);
$pattern = '/^.*號/u';
preg_match($pattern, $lot_number, $matches);
return $matches[0];
};
function split_city($lot_number) {
$pattern = '/臺北市|新北市|桃園市|臺中市|臺南市|高雄市|基隆市|新竹市|嘉義市|新竹縣|苗栗縣|彰化縣|南投縣|雲林縣|嘉義縣|屏東縣|宜蘭縣|花蓮縣|臺東縣|澎湖縣/u';
preg_match($pattern, $lot_number, $matches);
$city = $matches[0];
$from = '/'.preg_quote($city, '/').'/';
$other_lot_number = preg_replace($from, '', $lot_number, 1);
return Array($city, $other_lot_number);
};
function split_district($lot_number) {
$pattern = '/^\X+[鄉鎮市區]/u';
preg_match($pattern, $lot_number, $matches);
$district = $matches[0];
$other_lot_number = str_replace($district, '', $lot_number);
return Array($district, $other_lot_number);
};
function split_area($lot_number) {
$pattern = '/^\X+段/u';
preg_match($pattern, $lot_number, $matches);
$area = $matches[0];
$other_lot_number = str_replace($area, '', $lot_number);
return Array($area, $other_lot_number);
};
function split_number($lot_number) {
$pattern = '/^[\d-]+號/u';
preg_match($pattern, $lot_number, $matches);
$area = $matches[0];
$other_lot_number = str_replace($area, '', $lot_number);
return Array($area, $other_lot_number);
};
function split_lot_number($lot_number) {
$lot_number = clean_lot_number($lot_number);
$city_array = split_city($lot_number);
$district_array = split_district($city_array[1]);
$area_array = split_area($district_array[1]);
$split_number = split_number($area_array[1]);
return Array($city_array[0], $district_array[0], $area_array[0], $split_number[0]);
};
foreach ($test_array as $lot_number) {
echo $lot_number;
$result = split_lot_number($lot_number);
var_dump($result);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment