Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting a Shipping Address by Country in PHP (or Perl)

I have an HTML form that takes an input shipping address in parts (street address, city, state/province, postal code, and country). This form is then processed with PHP.

I'd like to convert this address into the correct format for the destination country. Are there any libraries or external services that I could use to do this conversion in PHP? If not, could I do it with Perl or a similar language?

like image 596
Eric Avatar asked Nov 18 '12 07:11

Eric


2 Answers

Never used it but Geo::PostalAddress is a good starting point. Useful links to regulations if nothing else.

Note that various shipping companies (Fedex, DHL etc) have their own rules for address format.

like image 66
Richard Huxton Avatar answered Oct 01 '22 02:10

Richard Huxton


In Perl you can use Class::Phrasebook. Using it is very easy.

use Class::Phrasebook;
my $pb = new Class::Phrasebook($log, "test.xml");
$pb->load("NL"); # using Dutch as the language
$phrase = $pb->get("ADDRESS", 
                   { street => "Chaim Levanon",
                     number => 88,
                     city   => "Tel Aviv" } );

Now in your case the shipping address will be dynamic (which will be provided by the user) so you'll have to do some more work. You can create a XML file, add dictionaries for all the countries, add phrases (street address, city, state/province, postal code) in each dictionary. Write country specific data in each phrase like "Street address: $street" for English dictionary, "adresse: $street" for French dictionary etc. And then access the dictionary according to the user's country.

More information at CPAN.

like image 34
Chankey Pathak Avatar answered Oct 01 '22 00:10

Chankey Pathak