转换国家代码
Posted
技术标签:
【中文标题】转换国家代码【英文标题】:Convert country codes 【发布时间】:2011-03-28 06:13:58 【问题描述】:国家代码有几种方法。
我有一个包含 3 个字符的代码列表,例如在此页面上:
http://www.fina.org/H2O/index.php?option=com_content&view=category&id=93:asia&Itemid=638&layout=default
有没有简单的方法将它们转换为 2 个字符?就像葡萄牙的“POR”中的“PT”。
2 个字符的标准 - http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
谢谢。
【问题讨论】:
好问题,但我认为你必须自己写一个或使用一些网络服务。 如果您在 gis.stackexchange.com 上发布此问题,您将更有可能获得具有直接映射 PT->PORTUGAL 的列表或数据库。那么您不必担心将 3 个字符代码转换为 2 个字符代码... 您可以使用这个网站:countryconversion.com 你解决了吗?我有同样的问题,我需要将 3 个字母转换为 2 个。如果您能帮上忙,不胜感激 【参考方案1】:您可以从http://country.io/data 获得一些有用的数据文件,它们会对您有所帮助:
http://country.io/iso3.json - ISO2 到 ISO3 国家代码的映射 http://country.io/names.json - ISO2 国家代码到国家名称的映射如果您只想从 3 个字母代码变为 2 个字母代码,您可以翻转第一张地图并使用它。您可以通过组合文件来创建一个直接从 3 个字母代码到国家名称的地图。这是一个简单的 PHP 示例:
$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"
【讨论】:
调用未定义函数 json_deocde() 抱歉,修正了错字。应该是 json_decode() 这个应该是答案 您是否还知道以不同语言提供“国家名称的 ISO2 国家代码”的列表?比如法语、德语、西班牙语等?【参考方案2】:我看到这个问题是七年前提出的。今天我遇到了类似的问题,并找到了一个很好的解决方案。希望这个答案对以后遇到同样问题的人有所帮助。
有一个单独的库可以使用https://github.com/thephpleague/iso3166
那么解决方案就很简单了。 $alpha3 是一个国家/地区的三个字符表示。而 alpha2 是国家/地区的两个字符表示。
$ composer require league/iso3166
$data = (new League\ISO3166\ISO3166)->alpha3($alpha3);
数据如下:
[
'name' => 'Netherlands',
'alpha2' => 'NL',
'alpha3' => 'NLD',
'numeric' => '528',
'currency' => [
'EUR',
]
]
$countryCodeInTwoChar = $data['alpha2']
【讨论】:
【参考方案3】:如果不进行实际查找,就没有简单的方法:AFG(阿富汗)变成 AF,而 AND(安道尔)变成 AD,BLR(白俄罗斯)变成 BY……所以你不能做任何简单的字符操作转换。
我的建议是使用国家/地区代码表,或在任何现有表中添加一个额外的列,以便您保存两个代码。
【讨论】:
正确,这就是为什么我建议在 gis.stackexchange.com 上发布这个问题 添加一个额外的列绝对是最好的方法【参考方案4】:上面的大多数其他答案都不是直接答案。让我试试
我已尝试使用 以下代码将 3 个字符转换为 2 个字符的国家/地区代码,使用 API:
<?php
$list=["BWA","SLV","TZA","BRB","IND","BES","ANT"];
$iso3=file_get_contents('http://country.io/iso3.json');//load the country codes
$iso3=json_decode($iso3,true);//convert json to associative array
foreach($list as $k)
if($k=="ANT")//not defined in code list
echo "AN";
else
echo array_search($k,$iso3);
echo "<br/>";
?>
提供的国家代码格式为 2-iso(key):3-iso(value)。所以我没有搜索键,而是按值搜索,如果成功则返回第一个对应的键。
输出
体重
SV
TZ
BB
输入
BQ
安
缺点 -
ANT
国家代码未在代码列表中定义。
参考 - array_search
【讨论】:
【参考方案5】:基于此处的其他信息,这里有一个完整的功能可供使用:
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;
【讨论】:
【参考方案6】:gis.stackexchange 中有类似的问题。https://gis.stackexchange.com/questions/603/is-a-country-state-city-database-available 我认为您将通过在 gis 中发布此问题来获得更多信息。 http://gis.stackexchange.com
【讨论】:
据我了解,Ignatz 需要将“POR”转换为“PT”,而不是将 IP 转换为位置 @Im0rtality - 是的,我知道,如果他得到 PT - PORTUGAL 映射数据库或列表,而不是转换它怎么办?这就是为什么我建议在 GIS 中发布这个问题。【参考方案7】:对于任何解决这个问题的人,我已经制作了一个 JSON 映射 alpha3 到 alpha2。 在这里下载:https://api.myjson.com/bins/1y2hn
因此,您只需将此 JSON 分配给一个对象(例如 countryCodes)。现在转换只是:
countryCodes["US"] //"USA"
【讨论】:
我发现这真的很有帮助。谢谢。【参考方案8】:虽然这可能是一个漫长而痛苦的方法,但编写一个可以永久保留的函数可能非常值得,也许这可以为您指明正确的方向:
<?php
function myCodes($in, $type)
$out = "";
$long = array('portugal', 'united kingdom');
$short = array('pt', 'uk');
$in = strtolower(trim($in));
switch($type)
case 'long':$out = str_replace($short, $long, $in);break;
case 'short':$out = str_replace($long, $short, $in);break;
echo $out;
echo myCodes('United Kingdom', 'short'); //this will echo 'uk'
echo myCodes('UK', 'long'); //this will echo 'united kingdom'
?>
这当然会有一些缺点,例如确保多头和空头的数组位置匹配,并且您还需要维护该功能。
【讨论】:
【参考方案9】:具有 ISO 3 代码但未排序的基于 Json 的数组
Paste Bin Link
和 PHP 数组 Paste BIN PHP Array
【讨论】:
【参考方案10】:$mapping['POR'] = 'PT'; $shortcode = $mapping[$longcode];
【讨论】:
【参考方案11】:在 ruby 中,您可以这样完成: (从http://download.geonames.org/export/dump/获取countryInfo.txt)
require 'csv'
countries_iso3_map =
CSV.foreach('countryInfo.txt',:col_sep=>' ',:row_sep =>:auto) do |row|
next if row[0][0] == '#' #ignore comments section
countries_iso3_map[row[0][0,2]]= row[1][0,3]
end
p countries_iso3_map['PT']
【讨论】:
【参考方案12】:没有简单的方法,因为国家名称中没有特定的方案。例如,PT
从 POR
到 Portugal
,对于其他国家/地区也可能不同。您可能希望创建一个数组来保存每个国家/地区的两个字母。
示例:
$countries = array('PT' => 'Portugal', 'UK' => 'United Kingdom');
【讨论】:
如果你有一个 2 位数 3 位数国家代码的数组,这很容易以上是关于转换国家代码的主要内容,如果未能解决你的问题,请参考以下文章