I need to match using regular expression in php addresses like:

  • 144 street, city, state zip/postal code
  • 144 street, apt #1, city, state zip/postal code
  • 144 street apt #1, city state zip/postal code

The zip/postal code can includes letters and/or numbers.

Here's what I tried:

print_r(preg_match('/^([0-9]+)\s([a-z]+)\s([a-z]+)\s([a-z]+)\s([a-z0-9]+)$/i', $t, $m));
print_r($m);

it outputs:

Array
(
    [0] => 123 asd asd asd 123
    [1] => 123
    [2] => street
    [3] => city
    [4] => state 
    [5] => zip
)

This works using spaces only. When I have a comma it does not work and it result in an empty array.

What can I do to also includes commas?

Edit
Report