Created
August 10, 2018 10:46
-
-
Save Ma-ve/6b0156acda2317628a4b45241b411d64 to your computer and use it in GitHub Desktop.
Nederlandse / Duitse adres regex
This file contains hidden or 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 | |
/** | |
* @param string $value | |
* @param int $index | |
* | |
* @return null|string | |
*/ | |
private function addressRegex($value, $index) { | |
preg_match("/^(\d*[\wäáàâåëéèêïíìîöóòôüúùûýÿÄÁÀÂËÉÈÊÏÍÌÎÖÓÒÔÜÚÙÛÝßñÑÇçšæÆ\d '\/\-\.]+)[,\s]+(\d+)\s*([\wäáàâåëéèêïíìîöóòôüúùûýÿÄÁÀÂËÉÈÊÏÍÌÎÖÓÒÔÜÚÙÛÝßñÑÇçšæÆ\d\-\/]*)$/", $value, $data); | |
return $data[$index] ?? null; | |
} | |
/** | |
* @param $value | |
* | |
* @return mixed|null | |
*/ | |
protected function streetName($value) { | |
return $this->addressRegex($value, 1); | |
} | |
/** | |
* @param $value | |
* | |
* @return mixed|null | |
*/ | |
protected function addressNumber($value) { | |
return $this->addressRegex($value, 2); | |
} | |
/** | |
* @param $value | |
* | |
* @return mixed|null | |
*/ | |
protected function addressNumberExtension($value) { | |
return $this->addressRegex($value, 3); | |
} | |
$lines = [ | |
'Hoofdstraat' => [ | |
'BezoekadresStraatnaam' => null, | |
'BezoekadresHuisnummer' => null, | |
'BezoekadresHuisnummerToevoeging' => null, | |
], | |
'Hoofdstraat 999' => [ | |
'BezoekadresStraatnaam' => 'Hoofdstraat', | |
'BezoekadresHuisnummer' => '999', | |
'BezoekadresHuisnummerToevoeging' => '', | |
], | |
'Hoofdstraat 999Z' => [ | |
'BezoekadresStraatnaam' => 'Hoofdstraat', | |
'BezoekadresHuisnummer' => '999', | |
'BezoekadresHuisnummerToevoeging' => 'Z', | |
], | |
'Hoofdstraat 999 Z' => [ | |
'BezoekadresStraatnaam' => 'Hoofdstraat', | |
'BezoekadresHuisnummer' => '999', | |
'BezoekadresHuisnummerToevoeging' => 'Z', | |
], | |
'Van Marwijkstraat 2' => [ | |
'BezoekadresStraatnaam' => 'Van Marwijkstraat', | |
'BezoekadresHuisnummer' => '2', | |
'BezoekadresHuisnummerToevoeging' => '', | |
], | |
'Van Marwijkstraat 2A' => [ | |
'BezoekadresStraatnaam' => 'Van Marwijkstraat', | |
'BezoekadresHuisnummer' => '2', | |
'BezoekadresHuisnummerToevoeging' => 'A', | |
], | |
'Van Marwijkstraat 2 A' => [ | |
'BezoekadresStraatnaam' => 'Van Marwijkstraat', | |
'BezoekadresHuisnummer' => '2', | |
'BezoekadresHuisnummerToevoeging' => 'A', | |
], | |
]; | |
foreach($lines as $line => $expected) { | |
self::assertEquals($expected['BezoekadresStraatnaam'], streetName($line)); | |
self::assertEquals($expected['BezoekadresHuisnummer'], addressNumber($line)); | |
self::assertEquals($expected['BezoekadresHuisnummerToevoeging'], addressNumberExtension($line)); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment