Skip to content

Instantly share code, notes, and snippets.

@R0B3RDV
Last active April 4, 2023 14:17
Show Gist options
  • Save R0B3RDV/e94c46c44a603e02afa2d226c6ef6367 to your computer and use it in GitHub Desktop.
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
<?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);
}
@TechOverflow
Copy link

Any idea how to get this working with foreign characters?
Uberstrasse 123 works, Überstrasse 123 doesn't.

@R0B3RDV
Copy link
Author

R0B3RDV commented Oct 23, 2020

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); }

@mPetzol
Copy link

mPetzol commented Aug 31, 2022

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