phpregexsplitpreg-splitzipcode

Split "City, State Zipcode" string between State and Zipcode


I have content like

San Clemente, CA 92673

or

Arlington, TX 76012-3693

And I need to split it into CityAndState and Zip.

I'm trying:

$moreAddressInfo = preg_split(
    '^[0-9.]{5,}^',
    $row2['cityAndZip'],
    null,
    PREG_SPLIT_DELIM_CAPTURE
);

I also tried without the function flag.

The CityState portion is getting returned just fine, but the main Zip info is missing.


Solution

  • Try this:

    $moreAddressInfo = preg_split('~\s+(?=[0-9]{5})~', $row2['cityAndZip']);
    

    So your delimiter is some whitespace that's followed by five digits.