php正则表达式城市国家邮政编码
这是我的一个数组php正则表达式城市国家邮政编码,php,regex,Php,Regex,这是我的一个数组 [citystatezip] => New York, NY 50805-2578 我试着把它做成下面的格式 [city] => New York [state] => NY [zip] => 50805-2578 我在php中使用了正则表达式,但什么也没有得到 谢谢你的帮助。试试这个正则表达式: /([^,]+),\s*(\w{2})\s*(\d{5}(?:-\d{4})?)/ 翻译成代码: $str = "New York, NY 50805
[citystatezip] => New York, NY 50805-2578
我试着把它做成下面的格式
[city] => New York
[state] => NY
[zip] => 50805-2578
我在php中使用了正则表达式,但什么也没有得到
谢谢你的帮助。试试这个正则表达式:
/([^,]+),\s*(\w{2})\s*(\d{5}(?:-\d{4})?)/
翻译成代码:
$str = "New York, NY 50805-2578";
preg_match("/([^,]+),\s*(\w{2})\s*(\d{5}(?:-\d{4})?)/", $str, $matches);
list($arr['addr'], $arr['city'], $arr['state'], $arr['zip']) = $matches;
print_r($arr);
给出:
Array
(
[zip] => 50805-2578
[state] => NY
[city] => New York
[addr] => New York, NY 50805-2578
)
使用此正则表达式:
/([^,]+),\s*(\w{2})\s*(\d{5}(?:-\d{4})?)/
- 存在一些输入验证(例如:要求输入的格式为:XXXXXXX,YY NNNNN-NNNN)
- 空格是可选的
- 拉链的最后4位数字是可选的
/([^,]+),\s*(\w{2})\s*(\d{5}(?:-\d{4})?)/
翻译成代码:
$str = "New York, NY 50805-2578";
preg_match("/([^,]+),\s*(\w{2})\s*(\d{5}(?:-\d{4})?)/", $str, $matches);
list($arr['addr'], $arr['city'], $arr['state'], $arr['zip']) = $matches;
print_r($arr);
给出:
Array
(
[zip] => 50805-2578
[state] => NY
[city] => New York
[addr] => New York, NY 50805-2578
)
使用此正则表达式:
/([^,]+),\s*(\w{2})\s*(\d{5}(?:-\d{4})?)/
- 存在一些输入验证(例如:要求输入的格式为:XXXXXXX,YY NNNNN-NNNN)
- 空格是可选的
- 拉链的最后4位数字是可选的
(.+?),(\w+)([-\d]+)
然后从捕获组获取信息。(.+?),(\w+)([-\d]+)
然后从捕获组获取信息。为什么不这样做呢
/(?P<city>[^,]+),\s*(?P<state>\w{2})\s*(?P<zip>\d{5}(?:-\d{4})?)/
因此,当你:
print_r($matches);
你会得到
Array
(
[city] => New York
[state] => NY
[zip] => 50805-2578
)
从NullUserException
中使用的主要表达式,所有功劳都归他所有。我只是缩短了流程。为什么不这样做呢
/(?P<city>[^,]+),\s*(?P<state>\w{2})\s*(?P<zip>\d{5}(?:-\d{4})?)/
因此,当你:
print_r($matches);
你会得到
Array
(
[city] => New York
[state] => NY
[zip] => 50805-2578
)
从
NullUserException
中使用的主要表达式,所有功劳都归他所有。我刚刚缩短了过程。您需要在正则表达式中使用取消reedy
/?
。您需要在正则表达式中使用取消reedy
/?
。