php 转换国家代码[已关闭]

lc8prwob  于 2023-01-24  发布在  PHP
关注(0)|答案(9)|浏览(208)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。

3天前关闭。
Improve this question
国家/地区代码有多种方法。
我有一个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
谢谢。

zzwlnbp8

zzwlnbp81#

您可以从http://country.io/data中获取一些有用的数据文件,这些文件将对您有所帮助:

如果你只想从3个字母的代码转换成2个字母的代码,你可以翻转第一张Map并使用它。你也可以通过组合文件来创建一张从3个字母的代码直接转换成国家名称的Map。下面是一个简单的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"
olmpazwi

olmpazwi2#

我看到七年前有人问过这个问题,今天我遇到了类似的问题,找到了一个很好的解决办法,希望这个答案对以后遇到同样问题的人有帮助。
有一个单独的库,可用于https://github.com/thephpleague/iso3166
那么解决方案就很简单了。$alpha3是一个国家的三个字符的表示,而alpha2是这个国家的两个字符的表示。

  1. $ composer require league/iso3166
  2. $data = (new League\ISO3166\ISO3166)->alpha3($alpha3);
    1.数据如下所示:
[
  'name' => 'Netherlands',
  'alpha2' => 'NL',
  'alpha3' => 'NLD',
  'numeric' => '528',
  'currency' => [
    'EUR',
    ]
]
  1. $countryCodeInTwoChar = $data['alpha2']
yhxst69z

yhxst69z3#

如果不进行实际的查找,就没有简单的方法:AFG(阿富汗)变成AF,AND(安道尔)变成AD,BLR(白俄罗斯)变成BY......所以你不能做任何简单的字符操作来转换。
我的建议是使用一个国家代码表,或者在任何现有的表中添加一个额外的列,这样您就可以同时拥有这两个代码。

bwleehnv

bwleehnv4#

以上其他答案大多数都不是直接答案,让我试试
我已尝试使用API以下代码将3个字符转换为2个字符的国家/地区代码

<?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)的形式,所以我不是搜索key,而是按value搜索,如果成功,则返回第一个对应的key。

    • 产出**

体重
标准体积
过渡区
BB型
输入
BQ
分析网

    • 缺点-**

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

u0sqgete

u0sqgete5#

基于这里的其他信息,这里有一个完整的功能准备好了:

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

9vw9lbht6#

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

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

?>

这当然会有一些缺点,比如要确保长数组和短数组的位置匹配,并且您还需要维护该函数。

ie3xauqp

ie3xauqp7#

$Map['POR'] = 'PT';
$短码= $Map[$长码];

ozxc1zmp

ozxc1zmp8#

在ruby中,你可以这样做:(从http://download.geonames.org/export/dump/获取国家信息. 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']
iqxoj9l9

iqxoj9l99#

这不是一个简单的方法,因为国家名称中没有特定的方案。例如,PT from POR for Portugal,这对于其他国家也可能不同。您可能需要创建一个数组,为每个国家保存两个字母。

    • 示例:**
$countries = array('PT' => 'Portugal', 'UK' => 'United Kingdom');

相关问题