Search code examples
phpcountry-codes

Convert country codes


There are several methods on country codes.

I have a list of codes with 3-characters, like on this page:

http://www.fina.org/H2O/index.php?option=com_content&view=category&id=93:asia&Itemid=638&layout=default

Is there a simple way to convert them to 2-characters? Like "PT" from "POR" for Portugal.

Standard for 2-characters - http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2

Thanks.


Solution

  • There is going to be no easy way because there is no particular scheme in the names of the country. For example PT from POR for Portugal and this can be different for other countries as well. You might want to create an array to hold two letters for each country.

    Example:

    $countries = array('PT' => 'Portugal', 'UK' => 'United Kingdom');