mOrloff
mOrloff

Reputation: 2617

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.

Upvotes: 0

Views: 404

Answers (1)

Alan Moore
Alan Moore

Reputation: 75222

Try this:

$moreAddressInfo = preg_split('~\s+(?=[0-9]{5})~', $row2['cityAndZip']);
  • \s+ matches one or more whitespace characters.

  • (?=[0-9]{5}) is a positive lookahead; it asserts that the next part of the string is five digits, but doesn't consume them.

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

Upvotes: 3

Related Questions