Last active
April 4, 2023 14:17
-
-
Save R0B3RDV/e94c46c44a603e02afa2d226c6ef6367 to your computer and use it in GitHub Desktop.
Function in PHP to split Dutch addresses as a string, to an array with street, number and addition apart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function split_street(string $streetStr) :array { | |
$aMatch = array(); | |
$pattern = '#^([\w[:punct:] ]+) (\d{1,5})\s?([\w[:punct:]\-/]*)$#'; | |
preg_match($pattern, $streetStr, $aMatch); | |
$street = $aMatch[1] ?? $streetStr; | |
$number = $aMatch[2] ?? ''; | |
$numberAddition = $aMatch[3] ?? ''; | |
return array('street' => $street, 'number' => $number, 'numberAddition' => $numberAddition); | |
} |
Any idea how to get this working with foreign characters?
Uberstrasse 123
works,Überstrasse 123
doesn't.
Hi,
Maybe this helps you:
function split_street(string $streetStr): array { $aMatch = array(); $pattern = '#^((?:[^\d\n]*)*)(\d{1,5})\s?([\w[:punct:]\-/]*)$#'; preg_match($pattern, $streetStr, $aMatch); $street = $aMatch[1] ?? $streetStr; $number = $aMatch[2] ?? ''; $numberAddition = $aMatch[3] ?? ''; return array('street' => $street, 'number' => $number, 'numberAddition' => $numberAddition); }
Any idea how to get this working with foreign characters?
Uberstrasse 123
works,Überstrasse 123
doesn't.
try the pattern as unicode:
$pattern = '/^([\w[:punct:] ]+) (\d{1,5})\s?([\w[:punct:]\-\/]*)$/u';
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any idea how to get this working with foreign characters?
Uberstrasse 123
works,Überstrasse 123
doesn't.