5

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)

bas
  • 628
  • 9
  • 22

1 Answers1

1

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

        }
    });
}
pouer
  • 61
  • 3
  • It's not perfect, but I guess this would work... Note that Geocoder is also implemented in a bunch of languages to be used on the server side. (within a 1000 hits per day limit, i believe) – bas Nov 26 '12 at 14:50
  • 2
    And you have to show a Google Map in order to use their service at all, according to their TOS. – Matt Nov 26 '12 at 14:55