将完整的美国街道地址解析为地址、城市、州、邮编
Parse Full USA Street Address into Address, City, State, Zip
我有每次都遵循这种格式的地址:
地址、城市、州邮政编码
示例:555 Test Drive, Testville, CA 98773
我想将地址解析为单独的变量:
- 地址
- 城市
- 州
- 邮编
我尝试了一些 preg_match 示例,但它们并不遵循我正在使用的相同模式。我要找的是正则表达式还是 preg_match?请帮忙!
你可以想出一些办法。喜欢:
(?P<address>[^,]+),\h+
(?P<city>[^,]+),\h+
(?P<state>\w+)\s+
(?P<zip>\w+)
参见a demo on regex101.com。
在 PHP
中,这将是:
$regex = '~
(?P<address>[^,]+),\h+ # everything that is not a comma, followed by a comma and horizontal whitespace
(?P<city>[^,]+),\h+ # the same as above
(?P<state>\w+)\h+ # word characters, followed by whitespace
(?P<zip>\w+)
~x'; # verbose mode
$string = '555 Test Drive, Testville, CA 98773';
preg_match($regex, $string, $match);
echo $match["address"];
# 555 Test Drive
参见a demo on ideone.com。
但是,如果逗号不总是存在,这会变得非常混乱(请提供更多输入字符串)。
如果您绝对肯定地址的格式将始终像您的示例一样,使用这些逗号,您有两个简单的选择。
选项 1:正则表达式
preg_match("/(.+), (\w+), (\w+) (\w+)/", $address, $matches);
list($original, $street, $city, $state, $zip) = $matches;
现在您有了自己的地址变量。
选项 2:爆炸
你也可以用explode()
把地址拆成碎片:
list($street, $city, $statezip) = explode(", ", $address);
list($state, $zip) = explode(" ", $statezip);
你也可以使用explode()
:
$full_address = '555 Test Drive, Testville, CA 98773';
$address = explode(',', $full_address)[0];
$city = explode(',', $full_address)[1];
$state = explode(' ', trim(explode(',', $full_address)[2]))[0];
$zip = explode(' ', trim(explode(',', $full_address)[2]))[1];
echo $address.'<br>';
echo $city.'<br>';
echo $state.'<br>';
echo $zip;
我有每次都遵循这种格式的地址:
地址、城市、州邮政编码
示例:555 Test Drive, Testville, CA 98773
我想将地址解析为单独的变量:
- 地址
- 城市
- 州
- 邮编
我尝试了一些 preg_match 示例,但它们并不遵循我正在使用的相同模式。我要找的是正则表达式还是 preg_match?请帮忙!
你可以想出一些办法。喜欢:
(?P<address>[^,]+),\h+
(?P<city>[^,]+),\h+
(?P<state>\w+)\s+
(?P<zip>\w+)
参见a demo on regex101.com。
在 PHP
中,这将是:
$regex = '~
(?P<address>[^,]+),\h+ # everything that is not a comma, followed by a comma and horizontal whitespace
(?P<city>[^,]+),\h+ # the same as above
(?P<state>\w+)\h+ # word characters, followed by whitespace
(?P<zip>\w+)
~x'; # verbose mode
$string = '555 Test Drive, Testville, CA 98773';
preg_match($regex, $string, $match);
echo $match["address"];
# 555 Test Drive
参见a demo on ideone.com。
但是,如果逗号不总是存在,这会变得非常混乱(请提供更多输入字符串)。
如果您绝对肯定地址的格式将始终像您的示例一样,使用这些逗号,您有两个简单的选择。
选项 1:正则表达式
preg_match("/(.+), (\w+), (\w+) (\w+)/", $address, $matches);
list($original, $street, $city, $state, $zip) = $matches;
现在您有了自己的地址变量。
选项 2:爆炸
你也可以用explode()
把地址拆成碎片:
list($street, $city, $statezip) = explode(", ", $address);
list($state, $zip) = explode(" ", $statezip);
你也可以使用explode()
:
$full_address = '555 Test Drive, Testville, CA 98773';
$address = explode(',', $full_address)[0];
$city = explode(',', $full_address)[1];
$state = explode(' ', trim(explode(',', $full_address)[2]))[0];
$zip = explode(' ', trim(explode(',', $full_address)[2]))[1];
echo $address.'<br>';
echo $city.'<br>';
echo $state.'<br>';
echo $zip;