0

I have content like

San Clemente, CA 92673

or

Arlington, TX 76012-3693

And I need to split it into CityAndState and Zip.
Some

I'm trying:

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

(also tried without the flag)
... the CityState portion is getting returned just fine, but the main Zip info is missing
(for the above two examples, $moreAddressInfo[1] would equal '' and '-3693', respectively).

Any pointers ?

mOrloff
  • 2,547
  • 4
  • 29
  • 48
  • `PREG_SPLIT_DELIM_CAPTURE` is useless until you have a group in the pattern. – hakre Apr 12 '12 at 23:44
  • since REGEX still seems like some kind of voodoo-magic to me, I'll have to take your word for it ;) ... any pointers/suggestions? – mOrloff Apr 12 '12 at 23:48
  • Just tell in your own words what you want that line of code to do for you. – hakre Apr 12 '12 at 23:49
  • I'd suggest you use preg match to find the position of the zip code, then split on that position and trim off the spaces and comma. – Sam Dufel Apr 12 '12 at 23:51
  • I want to break "Arlington, TX 76012-3693" into two vars .. 1) "Arlington, TX" .. 2) "76012-3693" – mOrloff Apr 12 '12 at 23:51
  • Ahah !!! preg match THEN split .. perfect, thanks. – mOrloff Apr 12 '12 at 23:59
  • There is no need to split after you used `preg_match`. `preg_match` will return all intermediate results, anyway. Just put the parts you need to output inside brackets like `~^(.*?)\s([\d\-]+)$~` or something. `preg_split` is used to split a string by matching a separator. You instead don't want to find this separator, but find the actual data inside the string. Thats what's `preg_match` is for. – Basti Apr 13 '12 at 00:01

1 Answers1

2

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.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156