-
-
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']); | |
} | |
} |
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:]\-/]*)$#';
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);
Question.
If you have something like
$testStreets[] = "1e Lange Heuvelweg13";
it does split into street and number.
How to fix this in the pattern?
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.
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
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.