Question

I finally have a question that isn't already answered on Stack Overflow for PHP.

I need to save the city, state, zip in different variables. Given the string

$new = "PALM DESERT             SD63376        "

I am trying to strip spaces that are grouped in 2 or more so I can keep a city 2 words when it is supposed to be. Beyond that I can figure out but all my searching either shows how to split a string in a different language (Java), or how to split at one space which I cannot do.

I'm trying to use

$addr = preg_split('#^.*(?=\s{2, })#', $new);

but I am not getting the string split between PALM DESERT AND "SD63376       ". Please help!

Was it helpful?

Solution

You can use the following:

$str = "PALM DESERT             SD63376        ";
// note that there are two spaces in the regex pattern
var_dump(preg_split('~  +~', $str, -1,  PREG_SPLIT_NO_EMPTY));

Output:

array(2) {
  [0] =>
  string(11) "PALM DESERT"
  [1] =>
  string(7) "SD63376"
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top