Convert country codes
Solution 1
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');
Solution 2
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"
Solution 3
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']
Solution 4
Without doing an actual lookup, there is no simple way: AFG (Afghanistan) becomes AF, while AND (Andorra) becomes AD, and BLR (Belarus) becomes BY... so you can't do any simple character manipulation to convert.
My suggestion would be to use a countrycode table, or add an extra column to any existing table, so that you hold both codes.
Solution 5
Building on the other info here, here's a complete function ready to go:
function convertCountryAlphas3To2($code='') {
$countries = json_decode('{"AFG":"AF","ALA":"AX","ALB":"AL","DZA":"DZ","ASM":"AS","AND":"AD","AGO":"AO","AIA":"AI","ATA":"AQ","ATG":"AG","ARG":"AR","ARM":"AM","ABW":"AW","AUS":"AU","AUT":"AT","AZE":"AZ","BHS":"BS","BHR":"BH","BGD":"BD","BRB":"BB","BLR":"BY","BEL":"BE","BLZ":"BZ","BEN":"BJ","BMU":"BM","BTN":"BT","BOL":"BO","BIH":"BA","BWA":"BW","BVT":"BV","BRA":"BR","VGB":"VG","IOT":"IO","BRN":"BN","BGR":"BG","BFA":"BF","BDI":"BI","KHM":"KH","CMR":"CM","CAN":"CA","CPV":"CV","CYM":"KY","CAF":"CF","TCD":"TD","CHL":"CL","CHN":"CN","HKG":"HK","MAC":"MO","CXR":"CX","CCK":"CC","COL":"CO","COM":"KM","COG":"CG","COD":"CD","COK":"CK","CRI":"CR","CIV":"CI","HRV":"HR","CUB":"CU","CYP":"CY","CZE":"CZ","DNK":"DK","DKK":"DK","DJI":"DJ","DMA":"DM","DOM":"DO","ECU":"EC","Sal":"El","GNQ":"GQ","ERI":"ER","EST":"EE","ETH":"ET","FLK":"FK","FRO":"FO","FJI":"FJ","FIN":"FI","FRA":"FR","GUF":"GF","PYF":"PF","ATF":"TF","GAB":"GA","GMB":"GM","GEO":"GE","DEU":"DE","GHA":"GH","GIB":"GI","GRC":"GR","GRL":"GL","GRD":"GD","GLP":"GP","GUM":"GU","GTM":"GT","GGY":"GG","GIN":"GN","GNB":"GW","GUY":"GY","HTI":"HT","HMD":"HM","VAT":"VA","HND":"HN","HUN":"HU","ISL":"IS","IND":"IN","IDN":"ID","IRN":"IR","IRQ":"IQ","IRL":"IE","IMN":"IM","ISR":"IL","ITA":"IT","JAM":"JM","JPN":"JP","JEY":"JE","JOR":"JO","KAZ":"KZ","KEN":"KE","KIR":"KI","PRK":"KP","KOR":"KR","KWT":"KW","KGZ":"KG","LAO":"LA","LVA":"LV","LBN":"LB","LSO":"LS","LBR":"LR","LBY":"LY","LIE":"LI","LTU":"LT","LUX":"LU","MKD":"MK","MDG":"MG","MWI":"MW","MYS":"MY","MDV":"MV","MLI":"ML","MLT":"MT","MHL":"MH","MTQ":"MQ","MRT":"MR","MUS":"MU","MYT":"YT","MEX":"MX","FSM":"FM","MDA":"MD","MCO":"MC","MNG":"MN","MNE":"ME","MSR":"MS","MAR":"MA","MOZ":"MZ","MMR":"MM","NAM":"NA","NRU":"NR","NPL":"NP","NLD":"NL","ANT":"AN","NCL":"NC","NZL":"NZ","NIC":"NI","NER":"NE","NGA":"NG","NIU":"NU","NFK":"NF","MNP":"MP","NOR":"NO","OMN":"OM","PAK":"PK","PLW":"PW","PSE":"PS","PAN":"PA","PNG":"PG","PRY":"PY","PER":"PE","PHL":"PH","PCN":"PN","POL":"PL","PRT":"PT","PRI":"PR","QAT":"QA","REU":"RE","ROU":"RO","RUS":"RU","RWA":"RW","BLM":"BL","SHN":"SH","KNA":"KN","LCA":"LC","MAF":"MF","SPM":"PM","VCT":"VC","WSM":"WS","SMR":"SM","STP":"ST","SAU":"SA","SEN":"SN","SRB":"RS","SYC":"SC","SLE":"SL","SGP":"SG","SVK":"SK","SVN":"SI","SLB":"SB","SOM":"SO","ZAF":"ZA","SGS":"GS","SSD":"SS","ESP":"ES","LKA":"LK","SDN":"SD","SUR":"SR","SJM":"SJ","SWZ":"SZ","SWE":"SE","CHE":"CH","SYR":"SY","TWN":"TW","TJK":"TJ","TZA":"TZ","THA":"TH","TLS":"TL","TGO":"TG","TKL":"TK","TON":"TO","TTO":"TT","TUN":"TN","TUR":"TR","TKM":"TM","TCA":"TC","TUV":"TV","UGA":"UG","UKR":"UA","ARE":"AE","GBR":"GB","USA":"US","UMI":"UM","URY":"UY","UZB":"UZ","VUT":"VU","VEN":"VE","VNM":"VN","VIR":"VI","WLF":"WF","ESH":"EH","YEM":"YE","ZMB":"ZM","ZWE":"ZW","GBP":"GB","RUB":"RU","NOK":"NO"}',true);
$out = $countries[$code];
return $out;
}
Related videos on Youtube
James
Updated on July 09, 2022Comments
-
James almost 2 years
There are several methods on country codes.
I have a list of codes with 3-characters, like on this page:
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.
-
Artefacto almost 14 yearsGood question, but I think you'll have to write one yourself or use some webservice.
-
-
Im0rtality almost 14 yearsFrom what I understood Ignatz needs to convert 'POR' to 'PT', not IP to location
-
CoderHawk almost 14 years@Im0rtality - yes I know, what if he get PT - PORTUGAL mapping database or list, rather than converting it? thats why I recommended posting this question in GIS.
-
CoderHawk almost 14 yearscorrect, thats why I recommend posting this question in gis.stackexchange.com
-
Tom Gullen almost 14 yearsAdding an extra column is definatly the best approach
-
cn007b almost 9 yearsCall to undefined function json_deocde()
-
Ben Dowling almost 9 yearsSorry, fixed the typo. Should have been json_decode()
-
Oleksandr IY about 8 yearsthis one should be answer
-
Adam over 7 yearsDo you also know of a list which provides 'ISO2 country codes to country names' in different languages? Like frensh, german, spanish, etc?
-
Tony Paternite over 7 yearsI actually found this really helpful. Thanks.