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:
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.
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);
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.
<?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/>";
}
?>
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.
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:
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.
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']
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']
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.
发布评论
评论(9)
您可以从 http://country.io/data 获取一些有用的数据文件,这些文件会对您有所帮助:
如果您只想从 3 个字母代码变为 2 个字母代码,您只需翻转首先绘制地图并使用它。不过,您可以通过组合文件来创建直接从 3 个字母代码到国家/地区名称的地图。这是一个简单的 PHP 示例:
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:
我发现这个问题是七年前提出的。今天我遇到了类似的问题,并找到了一个很好的解决方案。希望这个答案对以后遇到同样问题的人有所帮助。
有一个单独的库可以使用 https://github.com/thephpleague/iso3166
然后解决方案会很简单。 $alpha3 是一个国家/地区的三个字符表示。 alpha2 是国家/地区的两个字符表示。
$composer require league/iso3166
$data = (new League\ISO3166\ISO3166)->alpha3($alpha3);
数据如下所示:
<前><代码>[
'名字' => '荷兰',
'alpha2'=>; 'NL',
'alpha3'=>; '全国民主联盟',
'数字' => '528',
'货币' => [
'欧元',
]
]
$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.
$ composer require league/iso3166
$data = (new League\ISO3166\ISO3166)->alpha3($alpha3);
Data looks as follows:
$countryCodeInTwoChar = $data['alpha2']
基于此处的其他信息,这里有一个完整的函数可供使用:
Building on the other info here, here's a complete function ready to go:
如果不进行实际的查找,就没有简单的方法: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.
上面的大多数其他答案都不是直接答案。让我尝试一下,
我尝试使用 下面的代码将 3 个字符转换为 2 个字符的国家/地区代码 >API:
提供的国家代码,格式为2-iso(key):3-iso(value)。因此,我没有搜索键,而是按值搜索,如果成功则返回第一个相应的键。
输出
缺点 -
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:
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
Cons -
ANT
country code is not defined in code list.Reference - array_search
虽然这可能是一个漫长而痛苦的方法,但它可能非常值得您编写一个可以永远保留的函数,也许这可以为您指明正确的方向:
这当然有一些缺点,例如确保多头和空头数组在位置上匹配,并且您还需要维护该函数。
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:
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.
在 ruby 中你可以这样完成:
(从http://download.geonames.org/export/dump/获取countryInfo.txt )
In ruby you can get this done like this:
(get countryInfo.txt from http://download.geonames.org/export/dump/ )
没有简单的方法,因为国家名称中没有特定的方案。例如,
葡萄牙
的POR
中的PT
,对于其他国家/地区也可能有所不同。您可能想要创建一个数组来保存每个国家/地区的两个字母。示例:
There is going to be no easy way because there is no particular scheme in the names of the country. For example
PT
fromPOR
forPortugal
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: