国家/地区代码有多种方法。
我有一个包含3个字符的代码列表,如本页所示:
是否有一种简单的方法可以将它们转换为2个字符?就像葡萄牙“POR”中的“PT”一样。
2个字符的标准 - http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
感谢。
答案 0 :(得分:22)
您可以从http://country.io/data获得一些有用的数据文件,以帮助您:
如果你只想从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"
答案 1 :(得分:7)
我看到问题是七年前提出的。今天我有类似的问题,找到了一个很好的解决方案。希望这个答案对将来会遇到同样问题的人有所帮助。
有一个单独的库可以使用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']
答案 2 :(得分:3)
没有进行实际查找,没有简单的方法:AFG(阿富汗)成为AF,而AND(安道尔)成为AD,而BLR(白俄罗斯)成为BY ......所以你不能做任何简单的角色操控转换。
我的建议是使用国家/地区代码表,或在任何现有表格中添加额外的列,以便同时保存这两个代码。
答案 3 :(得分:2)
gis.stackexchange中存在类似的问题
https://gis.stackexchange.com/questions/603/is-a-country-state-city-database-available
我想你会通过在gis中发布这些问题来获得更多信息。
http://gis.stackexchange.com
答案 4 :(得分:2)
对于处理此问题的任何人,我已经将alpha3映射到alpha2。 在此下载:https://api.myjson.com/bins/1y2hn
因此,您只需将此JSON分配给对象(示例countryCodes)。现在转换只是:
countryCodes["US"] //"USA"
答案 5 :(得分:2)
上面的大多数其他答案都不是直接答案。让我试试
我已尝试以下代码,使用API将3个字符转换为2个字符的国家/地区代码:
@Provides provideRssListPresenter(RssService rssService) {
return new RssListPresenteR(rssService);
}
提供的国家/地区代码采用2-iso(密钥)形式:3-iso(值)。因此,我没有搜索密钥,而是按值搜索并在成功时返回第一个相应的密钥。
<强>输出强>
BW
SV
TZ
BB
在
BQ
AN
缺点 -
func cancelUploadVerificate() {
self.uploadVerificateRequest.session.getAllTasks { tasks in
tasks.forEach { $0.cancel() }
}
}
国家/地区代码未在代码列表中定义。
参考 - array_search
答案 6 :(得分:1)
虽然这可能是一个漫长而痛苦的方法,但在编写一个可以永远保留更多功能的功能时,你可能非常值得,也许这可以指向正确的方向:
<?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'
?>
这当然会有一些缺点,例如确保长和短的数组匹配位置,你也需要保持这个功能。
答案 7 :(得分:1)
答案 8 :(得分:0)
$ mapping ['POR'] ='PT';
$ shortcode = $ mapping [$ longcode];
答案 9 :(得分:0)
在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']
答案 10 :(得分:0)
在此基于其他信息,这是一个完整的功能,随时可以使用:
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;
}
答案 11 :(得分:-1)
没有简单的方法,因为该国的名称没有特别的计划。例如PT
来自POR
的{{1}},对于其他国家/地区也是如此。您可能想要创建一个数组,以便为每个国家/地区保留两个字母。
示例:强>
Portugal