PHP code example of gmaniac / us-address-normalization
1. Go to this page and download the library: Download gmaniac/us-address-normalization library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
gmaniac / us-address-normalization example snippets
use UsAddressNormalization\Normalizer;
$normalizer = new Normalizer();
$address = $normalizer->parse('204 southeast Smith Street Harrisburg, or 97446');
$address->getAddressComponents();
/* Returns:
[
"number" => "204",
"street" => "Smith",
"street_type" => "St",
"unit" => "",
"unit_prefix" => "",
"suffix" => "",
"prefix" => "SE",
"city" => "Harrisburg",
"state" => "OR",
"postal_code" => "97446",
"postal_code_ext" => null,
...
] */
$address->toString();
// "204 SE Smith St, Harrisburg, OR 97446"
use UsAddressNormalization\Normalizer;
$normalizer = new Normalizer();
$address1 = $normalizer->parse('204 southeast Smith Street Harrisburg, or 97446');
$address2 = $normalizer->parse('204 SE Smith St. Harrisburg, Oregon 97446');
$address3 = $normalizer->parse('207 SE Smith St. Harrisburg, Oregon 97446');
$address1->is($address2); // true
$address2->is($address3); // false
$address1->isSameStreet($address3); // true
// Compare hashes directly
$address1->getFullHash() === $address2->getFullHash(); // true
use UsAddressNormalization\Normalizer;
$normalizer = new Normalizer();
$address = $normalizer->parse('1234 Main St NE, Minneapolis, MN 55401');
// Hash excluding ZIP code (recommended for long-term matching since ZIPs can change)
$address->getHash();
// Full hash including ZIP code
$address->getFullHash();
// Street-only hash (for matching addresses on the same street)
$address->getStreetHash();
use UsAddressNormalization\SimpleAddress;
$address = new SimpleAddress('1234 Main St NE', null, 'Minneapolis', 'MN', '55401');
$address->getHash(); // hash minus zip
$address->getFullHash(); // hash including zip
// Static factory methods:
SimpleAddress::hashFromParts('1234 Main St NE', null, 'Minneapolis', 'MN', '55401');
SimpleAddress::fullHashFromParts('1234 Main St NE', null, 'Minneapolis', 'MN', '55401');
use UsAddressNormalization\Normalizer;
$normalizer = new Normalizer();
$address = $normalizer->parse('204 southeast Smith Street Harrisburg, or 97446');
$address->toString();
// or
(string) $address;
// "204 SE Smith St, Harrisburg, OR 97446"
$address->toArray();
// ['204 SE Smith St', 'Harrisburg, OR 97446']
use UsAddressNormalization\Normalizer;
$normalizer = new Normalizer();
$address = $normalizer->parseFromComponents(
'1234 Main St SE',
null, // address line 2
'Minneapolis',
'MN',
'55401'
);
use UsAddressNormalization\Normalizer;
// Strict mode (default: true)
// When disabled, unparseable addresses return SimpleAddress instead of false
$normalizer = new Normalizer(['strict_mode' => false]);
// Custom lookup tables
$normalizer = new Normalizer();
$normalizer->setDirectionalLookup(['north' => 'N', 'south' => 'S', ...]);
$normalizer->setStateCodesLookup(['california' => 'CA', ...]);
$normalizer->setStreetTypesLookup(['street' => 'st', 'avenue' => 'ave', ...]);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.