使用 PHP 将住所都道府県/市区町村/other 进行分割,
将其他按指定字符分割,本文为12,可自定义。
- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
function separate_address(string $address)
{
if(preg_match('@^(.{2,3}?[都道府県])(.+?郡.+?[町村]|.+?市.+?区|.+?[市区町村])(.+)@u', $address, $matches) != 1){
return [
'state' => null,
'city' => null,
'other' => null,
];
}
return [
'state' => $matches[1],
'city' => $matches[2],
'other' => $matches[3],
];
}
//------------------指定12字符分割Other字符串------------------------
function separate_address1(string $address1)
{
preg_match('@^(.{2,3}?[都道府県])(.+?郡.+?[町村]|.+?市.+?区|.+?[市区町村])(.+)@u', $address1, $matches1);
$j = 0;
$a[$j] =$matches1['1'].$matches1['2'].mb_substr($matches1['3'], 0, 12, 'utf-8');
$j++;
$a[$j] =mb_substr($matches1['3'], 12, 12, 'utf-8');
$j++;
$len =mb_strlen($matches1['3'],'utf-8')-24;
if ($len>0) {
$a[$j] =mb_substr($matches1['3'], 24, $len, 'utf-8');
}else{
$a[$j] ="";
}
echo '<pre>';
print_r($a);
}
echo '<pre>';
var_dump(separate_address('京都府京都市東山区清水1丁目294'));
var_dump(separate_address('東京都府中市宮西町2丁目24番地'));
var_dump(separate_address('不正な住所'));
// 市より先がない
var_dump(separate_address('東京都府中市'));
// 市名に村が入ってるのでうまく分けられていない。今回は非対応
var_dump(separate_address('東京都東村山市本町1丁目2番地3'));
echo '<br><br>-----------------------------------<br><br><pre>';
separate_address1('京都府京都市東山区清水1丁目294ははははははははマンション1010');
输出结果:
array(3) {
["state"]=>
string(9) "京都府"
["city"]=>
string(18) "京都市東山区"
["other"]=>
string(16) "清水1丁目294"
}
array(3) {
["state"]=>
string(9) "東京都"
["city"]=>
string(9) "府中市"
["other"]=>
string(24) "宮西町2丁目24番地"
}
array(3) {
["state"]=>
NULL
["city"]=>
NULL
["other"]=>
NULL
}
array(3) {
["state"]=>
NULL
["city"]=>
NULL
["other"]=>
NULL
}
array(3) {
["state"]=>
string(9) "東京都"
["city"]=>
string(6) "東村"
["other"]=>
string(27) "山市本町1丁目2番地3"
}
.-----------------------------------
Array
(
[0] => 京都府京都市東山区清水1丁目294はははは
[1] => ははははマンション101
[2] => 0
)
Enjoy!