6

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?

Eric
  • 63
  • 5

4 Answers4

3

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.

Richard Huxton
  • 21,516
  • 3
  • 39
  • 51
1

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.

Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
  • I think @Eric is asking about the address format. For example, German addresses have the postal code before the town/city, and UK addresses place it after. The Republic of Ireland does not use postcodes at all. – Richard Huxton Nov 18 '12 at 14:46
  • Damn! I misunderstood the question. Thanks Richard for providing information about the post code format, I didn't know that. – Chankey Pathak Nov 18 '12 at 16:31
1

For anyone still looking for a solution, there is now a professional made, well maintained open source library https://github.com/commerceguys/addressing that solves this exact problem.

Guntram
  • 414
  • 2
  • 5
  • 18
0

I've thought about this problem and I've decided that a file/database with address templates listed for each country is the best solution for me.

However, I'm certain that the other solutions given would work as well.

Eric
  • 63
  • 5