Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should I use to format an postal address in the format of the address country

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)

like image 693
bas Avatar asked Nov 12 '22 16:11

bas


1 Answers

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

        }
    });
}
like image 157
pouer Avatar answered Nov 15 '22 07:11

pouer