php地址的正则表达式

时间:2013-01-26 00:01:31

标签: php regex

我正在努力解决这个问题。如果有人能提供帮助就会很棒

  $address= str_replace('/\s+/', ' ', $address_line_1.' '.$address_line_2.', '.$town_village_city.', '.$state_province_county.', '.$postal_code.', '.$country);
    return str_replace('/,+/',', ',$address);

想要使用单个逗号单个空格格式化地址,即使完整地址也可能缺少地址的任何部分

不希望,,,,要返回,但在此实例中没有简单的字符

2 个答案:

答案 0 :(得分:1)

$address = implode(' ', array_filter(explode(' ',
   implode(', ', array_filter(explode(',', $address
))))));

这可以在没有正则表达式的情况下工作,但考虑到regexen的简单程度,我会坚持使用。

答案 1 :(得分:0)

如果您想使用正则表达式,请使用preg_replace()代替str_replace()

相关问题