转换国家代码

发布于 2024-09-13 15:58:16 字数 1429 浏览 5 评论 0 原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

赠我空喜 2024-09-20 15:58:16

您可以从 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"

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"
最初的梦 2024-09-20 15:58:16

我发现这个问题是七年前提出的。今天我遇到了类似的问题,并找到了一个很好的解决方案。希望这个答案对以后遇到同样问题的人有所帮助。

有一个单独的库可以使用 https://github.com/thephpleague/iso3166

然后解决方案会很简单。 $alpha3 是一个国家/地区的三个字符表示。 alpha2 是国家/地区的两个字符表示。

  1. $composer require league/iso3166
  2. $data = (new League\ISO3166\ISO3166)->alpha3($alpha3);
  3. 数据如下所示:

    <前><代码>[
    '名字' => '荷兰',
    'alpha2'=>; 'NL',
    'alpha3'=>; '全国民主联盟',
    '数字' => '528',
    '货币' => [
    '欧元',
    ]
    ]

  4. $countryCodeInTwoChar = $data['alpha2']

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']
夜巴黎 2024-09-20 15:58:16

基于此处的其他信息,这里有一个完整的函数可供使用:

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;
}

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;
}
︶葆Ⅱㄣ 2024-09-20 15:58:16

如果不进行实际的查找,就没有简单的方法:AFG(阿富汗)变为AF,AND(安道尔)变为AD,BLR(白俄罗斯)变为BY...因此您无法进行任何简单的字符操作来转换。

我的建议是使用国家/地区代码表,或在任何现有表中添加额外的列,以便您保存这两个代码。

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.

[浮城] 2024-09-20 15:58:16

上面的大多数其他答案都不是直接答案。让我尝试一下,

我尝试使用 下面的代码将 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

AN

缺点 -

ANT 国家/地区代码未在代码列表中定义。

参考 - array_search

Most of the other answers above are not direct answers. Let me try

I have tried below code to convert 3-characters to 2-characters country code using 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/>";
    }
?>

The country codes provided is in form of 2-iso(key):3-iso(value). So instead of searching for key, i searched by value and return the first corresponding key if successful.

Output

BW

SV

TZ

BB

IN

BQ

AN

Cons -

ANT country code is not defined in code list.

Reference - array_search

我喜欢麦丽素 2024-09-20 15:58:16

虽然这可能是一个漫长而痛苦的方法,但它可能非常值得您编写一个可以永远保留的函数,也许这可以为您指明正确的方向:

<?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'

?>

这当然有一些缺点,例如确保多头和空头数组在位置上匹配,并且您还需要维护该函数。

While this may be a lengthy and painful method, it may very well be worth your while writing a function that you can keep forever more, maybe this can point you in the right direction:

<?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'

?>

This will of course have a few drawbacks such as making sure that the arrays for long and short match up position wise, and you'll need to maintain the function as well.

薄凉少年不暖心 2024-09-20 15:58:16

$mapping['POR'] = 'PT';
$shortcode = $mapping[$longcode];

$mapping['POR'] = 'PT';
$shortcode = $mapping[$longcode];

背叛残局 2024-09-20 15:58:16

在 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']

In ruby you can get this done like this:
(get countryInfo.txt from http://download.geonames.org/export/dump/ )

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']
傻比既视感 2024-09-20 15:58:16

没有简单的方法,因为国家名称中没有特定的方案。例如,葡萄牙POR 中的 PT,对于其他国家/地区也可能有所不同。您可能想要创建一个数组来保存每个国家/地区的两个字母。

示例:

$countries = array('PT' => 'Portugal', 'UK' => 'United Kingdom');

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');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文