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

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?

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.

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.

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.

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.

Related

Convert a full address to a separated by, city, region, zip code, etc

I have been searched by the internet, however I didn't find a perfect solution, so I faith that someone already did something like this.
So my issue is, I'm using a webservice were you send the VAT number and if is valid you got the Company info. However the address received is the full address not divided by parts.
For example:
Google Ireland VAT is IE6388047V
I got:
Company Name: GOOGLE IRELAND LIMITED
Address: 3RD FLOOR ,GORDON HOUSE ,BARROW STREET ,DUBLIN 4
So what I need is something like this:
3RD FLOOR ,GORDON HOUSE ,BARROW STREET ,DUBLIN 4
Converts this to:
Address: Ringsend Post Office, Gordon House, Barrow St
City: Dublin 4
Country: Ireland
Someone can help me and make the day?
Thank you so much!
You can separate your address with array using PHP function explode(",",$address). It will separate your address after ", " and store in array...
Parsing addresses is challenging because of the many formats you will encounter, even within a single country. There are services that can parse the input and even verify whether the address exists. They usually return the address in components as well as a composed whole. Unfortunately, parsing international addresses is usually only available via a paid service. One such service is provided by smartystreets.com:
International API documentation
International output fields
(Disclosure: I'm a developer at smartystreets.)

PHP: dynamic delivery cost according to postcode

The task is just like the title says: implement in PHP a feature that dynamically sets the delivery cost according to given postcode for United Kingdom. It would be a part of an e-commerce application (precisely: prestashop). It should work more or less like on this page.
My question is: is there any tool I can use? The only thing I've found is a one-table "database" (basically to be downloaded in CSV). But there are 2 problems:
I'm missing relation between a small-scale region (like Aberdeen) and a big-scale region/county/country (like Scotland). I don't consider doing it manually - possibly there's something that could help.
However, as wikipedia says, Royal Mail has given some special postcodes that are not related to regions but to organisations. Therefore I can't be 100% sure that above CSV will cover all possible postcodes that a customer could submit.
Do you know any tool or webservice or whatever else that deals with such functionalities?
Why not use a postcode database from an official source, such as Ordnance Survey's CodePoint Open - ordnancesurvey.co.uk/oswebsite/products/code-point-open - which is also free, and updated four times a year.
CodePoint Open doesn't include the 'specials', because they're not geographically tide (so the valid "SAN TA1" postcode isn't included), but do you really expect to be delivering to the headquarters of the FA or any of those "own postcode" organisations? If you do need those, then you can always add them manually.

PHP - Postcode check if it's covered

I am currently developing a website for an electrical company. They would like some sort of postcode check on there. It would somehow work like this:
User enters postcode
See if we cover it
display results.
But I have never worked with postcodes before. How would I be able to check whether they cover it. I obviously need some sort of database listing the postcode or area they cover. But how would I also check if the postcode is valid.
The postcode lookup is obviously to see if the electrical company covers the user's area.
Thanks in advance.
I think instead of using a database to search your results, you would be better of looking at geo location, and using a 3rd party to calculate everything for you.
Google and Sony both provide Geo Location platforms
Sony has: http://www.placeengine.com/en
Google has: http://code.google.com/apis/maps/documentation/distancematrix/
I obviously need some sort of database listing the postcode or area they cover.
Yes. Then do a simple look up.
But how would I also check if the postcode is valid.
You would have to have a database listing all possible post codes and do a similar check.
I don't know which country you are in, but in most countries there is no programmatical way of determining wether a postcode is covered by a service or not.
Also no way of knowing wether it is valid or not without consulting a database that contains coverage.
There are databases available with postal codes, here is a dutch one: http://www.postcode.nl/index/269/1/0/overzicht-producten-en-diensten.html
You can validate the postal code using a regular expression.
The electrical company could surely give you a list of postcodes they do cover? Then it is a simple string matching from there....
I assume you are interested in the UK. To see if a postcode is valid grab the free CodePoint open data set from OS: http://www.ordnancesurvey.co.uk/oswebsite/products/code-point-open/
You could pull that into a DG and cross check user input. Just remember that this data is based on the Royal Mail PAF, so do not assume it is 100% accurate. Build a bit of flexibility/fault tolerance into your code.
If the client wants a specific radius covered, you coudl also use the OS data for distance calculations... and it is all FREE, as in both freedom and free beer :-)
As a first step you could check if the postcode is in a valid format: http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom#Validation - this check will pre-filter input for you.

Performing bulk language translation in a large database

My application has a 2.7 million records table with the list of all cities and villages in the world (provided by GeoNames.org). Each city has it's name in the native language. This database will be searched in an auto-complete form, but users should be able to type the city name in their own language (primarily Portuguese, since this will be a Brazilian website) and be able to locate the city (at least the most important ones).
For instance: Munich is a well-known German city. However, in the GeoNames database, it is registered as "München", the native German name. GeoNames provides an english representation of the name, but that basically strips the special characters of the city name (in this case, München becomes Munchen, not Munich).
Is there a way or service I can use to translate each of these cities names into Portuguese (or at the very least, English) and cache them in my database? I've looked into the Google Translation API, but in their TOS, automated processes are forbidden.
Thanks in advance.
A solution I found was parsing the WordPress translation files, it includes continents & cities in one of the language files.
Find the language you want here:
http://codex.wordpress.org/WordPress_in_Your_Language
Go to the language you want and download WordPress
Inside the zip goto: /wp-content/languages/
and open continents & cities file eg. "continents-cities-nl_NL.po"
In that file you'll have all the translated city names you can easily parse into your DB
I'm currently parsing the cities in PHP when I'm finished I can post the translated files here if you want.
How about parsing Wikipedia? I often use the "Languages" menu as a dictionary. It would take some time but since this is a one-time operation it doesn't matter that much.

PHP regexp US address

I'm trying to see what would be a good way to validate a US address, I know that there might be not a proper way of doing this, but I'm going for the basic way: #, Street name, City, State, and Zip Code.
Any ideas will be appreciate it. Thanks
Don't try. Somebody is likely to have a post office box, or an apartment number etc., and they will be really irate with you. Even a "normal" street name can have numbers, like 125th Street (and many others) in New York City. Even a suburb can have some numbered streets.
And city names can have spaces.
Ask the user to enter parts of the address in separate fields (Street name, City, State, and Zip Code) and use whatever validation appropriate for such a field. This is the general practice.
Alternatively, if you want simplest of regex that matches for four strings separated by three commas, try this:
/^(.+),([^,]+),([^,]+),([^,]+)$/
If things match, you can use additional pattern matching to check components of the address. There is no possible way to check the street address validity but you might be able to text postal codes and state codes.
There are way too many variations in address to be able to do this using regular expressions. You're better off finding a web service that can validate addresses. USPS has one - you'll have to request permission to use it.
I agree with salman: have user enter the data in different fields (one for zip, one for state, one for city, and one for the #/street name. Use a different regex for each field. For the street #/name the best expression i came up with was
/^[0-9]{1,7} [a-zA-z0-9]{2,35}\a*/
This is not a bulletproof solution but the assumption is that an address begins with a numeric for the street number and ends with a zip code which can either be 5 or 9 numbers.
([0-9]{1,} [\s\S]*? [0-9]{5}(?:-[0-9]{4})?)
Like I said, it's not bulletproof, but I've used it with marginal success in the past.
Over here in New Zealand, you can license the official list of postal addresses from New Zealand Post - giving you the data needed to populate a table with every valid postal address in New Zealand.
Validating against this list is a whole lot easier than trying to come up with a Regex - and the accuracy is much much higher as well, as you end up with three cases:
The address you're validating is in the list, so you know it is a real address
The address you're validating is very similar to one in the list, so you know it is probably a real address
The address you're validating is not similar in the list, so it may or may not be real.
The best you'll get with a RegEx is
The address you're validating matches the regex, so it might be a real address
The address you're validating does not match the regex, so it might not be a real address
Needing to know postal addresses is a pretty common situation for many businesses, so I believe that licensing a list will be possible in most areas.
The only sticky bit will be pricing.

Categories