I need to format postal addresses into the format of the country and am looking for some PHP library that can do that. I work with zend framework 2 which uses Locale but unfortunately that does not seem to be able to handle this at this time.
examples
NL RECIPIENT
STREET_NAME HOUSE_NUMBER
POSTAL_CODE LOCALITY
COUNTRY
US RECIPIENT
HOUSE_NUMBER STREET_NAME [STREET_TYPE] [STREET_DIRECTION] [BUILDING] [FLOOR] [APARTMENT]
LOCALITY PROVINCE_ABBREVIATION POSTAL_CODE
COUNTRY
UK RECIPIENT
[FLOOR] [APARTMENT]
[BUILDING]
[HOUSE_NUMBER] STREET_NAME
[DEPENDENT_LOCALITY]
LOCALITY
POSTAL_CODE
(source http://www.addressdoctor.com/en/countries-data/address-formats.html)
I had to face the same issue while making an international website.
The solution was to print a string made of all concatenated parts of the address and when possible use the Google Map API to properly format the address.
var g = new google.maps.Geocoder();
function format_address(raw_address) {
g.geocode({address: raw_address}, function (a, status) {
if (status === "OK") {
var splt = a[0].formatted_address.split(new RegExp(",", "g"));
var i = 0;
var res = "";
for(i = 0; i < splt.length-1; i++)
res += $.trim(splt[i]) + '<br/>';
res += $.trim(splt[splt.length-1]);
$('#address').html(res);
}
else if (status == "OVER_QUERY_LIMIT") {
//wait 500ms and try again
}
else {
$('#address').(raw_adress);
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With