Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • 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']);
}
}
@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