Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save benvds/350404 to your computer and use it in GitHub Desktop.
Save benvds/350404 to your computer and use it in GitHub Desktop.
/**
* @brief Splits an address string containing a street, number and number addition
*
* @param $streetStr string An address string containing a street, number (optional) and number addition (optional)
*
* @return array Data array with the following keys: street, number and numberAddition.
*/
private function split_street($streetStr) {
$aMatch = array();
$pattern = '#^([\w[:punct:] ]+) ([0-9]{1,5})([\w[:punct:]\-/]*)$#';
$matchResult = preg_match($pattern, $streetStr, $aMatch);
$street = (isset($aMatch[1])) ? $aMatch[1] : '';
$number = (isset($aMatch[2])) ? $aMatch[2] : '';
$numberAddition = (isset($aMatch[3])) ? $aMatch[3] : '';
return array('street' => $street, 'number' => $number, 'numberAddition' => $numberAddition);
}
private function test_split_street() {
$testStreets = array();
$testStreets[] = "laan 1933 2";
$testStreets[] = "18 septemberplein 12";
$testStreets[] = "kerkstraat 42-f3";
$testStreets[] = "Kerk straat 2b";
$testStreets[] = "1, rue de l'eglise"; // jammer-de-bammer
$testStreets[] = "42nd street, 1337a";
$testStreets[] = "1e constantijn huigensstraat 1b";
$testStreets[] = "Heuvel, 2a";
$testStreets[] = "Heuvel 13";
$testStreets[] = "3-koningenstraat, 21 13b"; // jammer-de-bammer
$testStreets[] = "glaslaan 2, gebouw SWA 71";
for ($i = 0, $totalTestStreets = count($testStreets); $i < $totalTestStreets; $i++) {
$address = $this->split_street($testStreets[$i]);
debug($testStreets[$i]);
debug($address['street']);
debug($address['number']);
debug($address['numberAddition']);
}
}
@marnickmenting
Copy link

Great gist.

I changed line 14 to: $street = (isset($aMatch[1])) ? $aMatch[1] : $streetStr;

If there is no match, it returns at least the full known string as fallback. This is not applicable for every situation of course. I had the situation where only the street is filled in, which is not a correct address, but I need it to return the street name anyway.

@marnickmenting
Copy link

Also, I added an optional space between number and addition. Not sure if it works for all cases, but in my case it did. This is the updated regex: $pattern = '#^([\w[:punct:] ]+) ([0-9]{1,5})\s?([\w[:punct:]\-/]*)$#';

@R0B3RDV
Copy link

R0B3RDV commented May 7, 2020

Very nice thanx.

The variable $matchResult is never actually never used: so to make it even shorter, you can replace it with:
preg_match($pattern, $streetStr, $aMatch);

@PaccoRobin
Copy link

Question.
If you have something like
$testStreets[] = "1e Lange Heuvelweg13";
it does split into street and number.
How to fix this in the pattern?

@rubentebogt
Copy link

rubentebogt commented Jun 9, 2020

I created a repo on PHP for this matter, see: https://github.com/WebRTB/address-splitter. it is still w.i.p. though pull requests are more then welcome.

@R0B3RDV
Copy link

R0B3RDV commented Jul 3, 2020

hi, i've created a simplified version of this one, with some minor bug fixes, maybe it can help someone with further development. https://gist.github.com/R0B3RDV/e94c46c44a603e02afa2d226c6ef6367

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment