Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 843
James Avatar asked Aug 11 '10 08:08

James


People also ask

How do you find the country code of a country?

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.

Is there a 3 letter country code?

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.

Is there a 2 letter country code?

Country Code IS Country code according to ISO-3166 Alpha-2IS is the two-letter country abbreviation for Iceland.


2 Answers

There are some useful data files that you can get from http://country.io/data that'll help you:

  • http://country.io/iso3.json - a map of ISO2 to ISO3 country codes
  • http://country.io/names.json - a map of ISO2 country codes to country names

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"
like image 149
Ben Dowling Avatar answered Sep 23 '22 17:09

Ben Dowling


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.

  1. $ composer require league/iso3166
  2. $data = (new League\ISO3166\ISO3166)->alpha3($alpha3);
  3. Data looks as follows:

    [
      'name' => 'Netherlands',
      'alpha2' => 'NL',
      'alpha3' => 'NLD',
      'numeric' => '528',
      'currency' => [
        'EUR',
        ]
    ]
    
  4. $countryCodeInTwoChar = $data['alpha2']
like image 38
Turdaliev Nursultan Avatar answered Sep 20 '22 17:09

Turdaliev Nursultan