viison / address-splitter
Tries to split an address line into street name, house number and other additional information like building, apartment information etc.
Requires
- php: >=5.3.2
Requires (Dev)
- phpunit/phpunit: ~4.0
This package is not auto-updated.
Last update: 2024-11-09 17:59:53 UTC
README
Tries to split an address line into street name, house number and other additional information like building, apartment information etc.
Installation
This project can be installed via Composer:
$ composer require viison/address-splitter
Usage
The AddressSplittingService only contains one static function splitAddress
that performs the address splitting.
The function expects one parameter, which is the address line. The function returns an associative array with the
keys additionToAddress1
, streetName
, houseNumber
and additionToAddress2
. additionToAddress1
and additionToAddress2
contain information given in front of or after the street name & house number, respectively.
Example
You can use the address splitting service as follows:
var_dump(\VIISON\AddressSplitter\AddressSplitter::splitAddress('Pallaswiesenstr. 57B App. 235'));
The output of this command will be:
array(4) { ["additionToAddress1"]=> string(0) "" ["streetName"]=> string(16) "Pallaswiesenstr." ["houseNumber"]=> string(2) "57B" ["houseNumberParts"]=> array(2) { ["base"]=> string(2) "57" ["extension"]=> string(1) "B" } ["additionToAddress2"]=> string(8) "App. 235" }
Supported Address Formats
We try to support all address formats used world-wide. E.g., the address splitter will work no matter if the house number is given in front of or after the street name.
Here is a number of examples of addresses and how their splitted representation looks like:
Unit Tests
The examples above and even more exemplary address lines are part of our unit tests. The unit tests can be run on the following site: https://regex101.com/r/vO5fY7/5
You can also run the tests via PHPUnit from the command line:
$ phpunit
If you don't have PHPUnit installed globally, run composer install
first.
Further Information
The need for this functionality came up when we noticed that Shopware 5 does not contain individual fields for the street name and the house number anymore. Nevertheless, we needed to have these separated for our DHL Adapter, because the DHL API expects them to be passed individually.
More background information on how this implementation came together can be found in our blog post.
Adresstrennung
Dieses Projekt ermöglicht es, eine Adresszeile in Straßennamen und Hausnummer sowie weitere Angaben wie z.B. Gebäude- oder Appartmentinformationen zu trennen. Unterstützt werden eine Vielzahl an Adressformaten, wie sie weltweit verwendet werden. So ist es z.B. unerheblich, ob die Hausnummer auf die Straße folgt oder ihr voran steht.
License
viison/address-splitter is licensed under the MIT license. See the LICENSE
file for more details.