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.
name = locale. getDisplayCountry(); // Map all country names and codes in key - value pairs. countryMap. put(name, code); } // Return the country code for the given country name using the map. // Here you will need some validation or better yet // a list of countries to give to user to choose from.
ISO 3166-1 alpha-3 codes are three-letter country codes defined in ISO 3166-1, part of the ISO 3166 standard published by the International Organization for Standardization (ISO), to represent countries, dependent territories, and special areas of geographical interest.
Country Code IS Country code according to ISO-3166 Alpha-2IS is the two-letter country abbreviation for Iceland.
There are some useful data files that you can get from http://country.io/data that'll help you:
If you just want to go from 3 letter codes to 2 letter codes you can just flip the first map and use that. You could create a map that goes directly from 3 letter codes to country names by combing the files though. Here's a simple PHP example:
$codes = json_decode(file_get_contents('http://country.io/iso3.json'), true);
$names = json_decode(file_get_contents('http://country.io/names.json'), true);
$iso3_to_name = array();
foreach($codes as $iso2 => $iso3) {
$iso3_to_name[$iso3] = $names[$iso2];
}
echo $names("PL"); // => "Poland"
echo $iso3_to_map("POL"); // => "Poland"
I see that the question was asked seven years ago. Today I had the similar issue and found one good solution. Hope that this answer will be helpful to others who will have the same issue in the future.
There is a separate library which can be used https://github.com/thephpleague/iso3166
Then the solution would be straightforward. $alpha3 is the three char representation of a country. And alpha2 is two char representation of the country.
$ composer require league/iso3166
$data = (new League\ISO3166\ISO3166)->alpha3($alpha3);
Data looks as follows:
[
'name' => 'Netherlands',
'alpha2' => 'NL',
'alpha3' => 'NLD',
'numeric' => '528',
'currency' => [
'EUR',
]
]
$countryCodeInTwoChar = $data['alpha2']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With