24

I'm triying to convert, using PHP, 2 characters country code (ISO 3166-1) to country names, or intl country codes, so I can translate the country name.

Using Locale::getDisplayRegion needs locale codes for languages (nl_NL) and all I have is the 2 characters country code. I don't want to rely on Webservices, but I can think on the geoip extension (I can't find a solution using it anyway)

Is there any way to get the 2 chars country code and convert it to localiced country name in PHP without using webservices or external resources?

(I was using zend_locale some time ago, but zf2 does not have that functionality)

4

7 回答 7

49

您不必为第一个参数提供完整的语言环境。这使得该函数可用于从任何语言环境获取国家名称:

<?php
var_dump(Locale::getDisplayRegion('-US', 'fr'));

//Returns
string 'États-Unis' (length=11)
?>

http://php.net/manual/en/locale.getdisplayregion.php#115523

于 2014-10-10T20:15:54.167 回答
36

这里有数组的函数:

//Edited by rezker (http://www.rezker.com)
    function code_to_country( $code ){

    $code = strtoupper($code);

    $countryList = array(
        'AF' => 'Afghanistan',
        'AX' => 'Aland Islands',
        'AL' => 'Albania',
        'DZ' => 'Algeria',
        'AS' => 'American Samoa',
        'AD' => 'Andorra',
        'AO' => 'Angola',
        'AI' => 'Anguilla',
        'AQ' => 'Antarctica',
        'AG' => 'Antigua and Barbuda',
        'AR' => 'Argentina',
        'AM' => 'Armenia',
        'AW' => 'Aruba',
        'AU' => 'Australia',
        'AT' => 'Austria',
        'AZ' => 'Azerbaijan',
        'BS' => 'Bahamas the',
        'BH' => 'Bahrain',
        'BD' => 'Bangladesh',
        'BB' => 'Barbados',
        'BY' => 'Belarus',
        'BE' => 'Belgium',
        'BZ' => 'Belize',
        'BJ' => 'Benin',
        'BM' => 'Bermuda',
        'BT' => 'Bhutan',
        'BO' => 'Bolivia',
        'BA' => 'Bosnia and Herzegovina',
        'BW' => 'Botswana',
        'BV' => 'Bouvet Island (Bouvetoya)',
        'BR' => 'Brazil',
        'IO' => 'British Indian Ocean Territory (Chagos Archipelago)',
        'VG' => 'British Virgin Islands',
        'BN' => 'Brunei Darussalam',
        'BG' => 'Bulgaria',
        'BF' => 'Burkina Faso',
        'BI' => 'Burundi',
        'KH' => 'Cambodia',
        'CM' => 'Cameroon',
        'CA' => 'Canada',
        'CV' => 'Cape Verde',
        'KY' => 'Cayman Islands',
        'CF' => 'Central African Republic',
        'TD' => 'Chad',
        'CL' => 'Chile',
        'CN' => 'China',
        'CX' => 'Christmas Island',
        'CC' => 'Cocos (Keeling) Islands',
        'CO' => 'Colombia',
        'KM' => 'Comoros the',
        'CD' => 'Congo',
        'CG' => 'Congo the',
        'CK' => 'Cook Islands',
        'CR' => 'Costa Rica',
        'CI' => 'Cote d\'Ivoire',
        'HR' => 'Croatia',
        'CU' => 'Cuba',
        'CY' => 'Cyprus',
        'CZ' => 'Czech Republic',
        'DK' => 'Denmark',
        'DJ' => 'Djibouti',
        'DM' => 'Dominica',
        'DO' => 'Dominican Republic',
        'EC' => 'Ecuador',
        'EG' => 'Egypt',
        'SV' => 'El Salvador',
        'GQ' => 'Equatorial Guinea',
        'ER' => 'Eritrea',
        'EE' => 'Estonia',
        'ET' => 'Ethiopia',
        'FO' => 'Faroe Islands',
        'FK' => 'Falkland Islands (Malvinas)',
        'FJ' => 'Fiji the Fiji Islands',
        'FI' => 'Finland',
        'FR' => 'France, French Republic',
        'GF' => 'French Guiana',
        'PF' => 'French Polynesia',
        'TF' => 'French Southern Territories',
        'GA' => 'Gabon',
        'GM' => 'Gambia the',
        'GE' => 'Georgia',
        'DE' => 'Germany',
        'GH' => 'Ghana',
        'GI' => 'Gibraltar',
        'GR' => 'Greece',
        'GL' => 'Greenland',
        'GD' => 'Grenada',
        'GP' => 'Guadeloupe',
        'GU' => 'Guam',
        'GT' => 'Guatemala',
        'GG' => 'Guernsey',
        'GN' => 'Guinea',
        'GW' => 'Guinea-Bissau',
        'GY' => 'Guyana',
        'HT' => 'Haiti',
        'HM' => 'Heard Island and McDonald Islands',
        'VA' => 'Holy See (Vatican City State)',
        'HN' => 'Honduras',
        'HK' => 'Hong Kong',
        'HU' => 'Hungary',
        'IS' => 'Iceland',
        'IN' => 'India',
        'ID' => 'Indonesia',
        'IR' => 'Iran',
        'IQ' => 'Iraq',
        'IE' => 'Ireland',
        'IM' => 'Isle of Man',
        'IL' => 'Israel',
        'IT' => 'Italy',
        'JM' => 'Jamaica',
        'JP' => 'Japan',
        'JE' => 'Jersey',
        'JO' => 'Jordan',
        'KZ' => 'Kazakhstan',
        'KE' => 'Kenya',
        'KI' => 'Kiribati',
        'KP' => 'Korea',
        'KR' => 'Korea',
        'KW' => 'Kuwait',
        'KG' => 'Kyrgyz Republic',
        'LA' => 'Lao',
        'LV' => 'Latvia',
        'LB' => 'Lebanon',
        'LS' => 'Lesotho',
        'LR' => 'Liberia',
        'LY' => 'Libyan Arab Jamahiriya',
        'LI' => 'Liechtenstein',
        'LT' => 'Lithuania',
        'LU' => 'Luxembourg',
        'MO' => 'Macao',
        'MK' => 'Macedonia',
        'MG' => 'Madagascar',
        'MW' => 'Malawi',
        'MY' => 'Malaysia',
        'MV' => 'Maldives',
        'ML' => 'Mali',
        'MT' => 'Malta',
        'MH' => 'Marshall Islands',
        'MQ' => 'Martinique',
        'MR' => 'Mauritania',
        'MU' => 'Mauritius',
        'YT' => 'Mayotte',
        'MX' => 'Mexico',
        'FM' => 'Micronesia',
        'MD' => 'Moldova',
        'MC' => 'Monaco',
        'MN' => 'Mongolia',
        'ME' => 'Montenegro',
        'MS' => 'Montserrat',
        'MA' => 'Morocco',
        'MZ' => 'Mozambique',
        'MM' => 'Myanmar',
        'NA' => 'Namibia',
        'NR' => 'Nauru',
        'NP' => 'Nepal',
        'AN' => 'Netherlands Antilles',
        'NL' => 'Netherlands the',
        'NC' => 'New Caledonia',
        'NZ' => 'New Zealand',
        'NI' => 'Nicaragua',
        'NE' => 'Niger',
        'NG' => 'Nigeria',
        'NU' => 'Niue',
        'NF' => 'Norfolk Island',
        'MP' => 'Northern Mariana Islands',
        'NO' => 'Norway',
        'OM' => 'Oman',
        'PK' => 'Pakistan',
        'PW' => 'Palau',
        'PS' => 'Palestinian Territory',
        'PA' => 'Panama',
        'PG' => 'Papua New Guinea',
        'PY' => 'Paraguay',
        'PE' => 'Peru',
        'PH' => 'Philippines',
        'PN' => 'Pitcairn Islands',
        'PL' => 'Poland',
        'PT' => 'Portugal, Portuguese Republic',
        'PR' => 'Puerto Rico',
        'QA' => 'Qatar',
        'RE' => 'Reunion',
        'RO' => 'Romania',
        'RU' => 'Russian Federation',
        'RW' => 'Rwanda',
        'BL' => 'Saint Barthelemy',
        'SH' => 'Saint Helena',
        'KN' => 'Saint Kitts and Nevis',
        'LC' => 'Saint Lucia',
        'MF' => 'Saint Martin',
        'PM' => 'Saint Pierre and Miquelon',
        'VC' => 'Saint Vincent and the Grenadines',
        'WS' => 'Samoa',
        'SM' => 'San Marino',
        'ST' => 'Sao Tome and Principe',
        'SA' => 'Saudi Arabia',
        'SN' => 'Senegal',
        'RS' => 'Serbia',
        'SC' => 'Seychelles',
        'SL' => 'Sierra Leone',
        'SG' => 'Singapore',
        'SK' => 'Slovakia (Slovak Republic)',
        'SI' => 'Slovenia',
        'SB' => 'Solomon Islands',
        'SO' => 'Somalia, Somali Republic',
        'ZA' => 'South Africa',
        'GS' => 'South Georgia and the South Sandwich Islands',
        'ES' => 'Spain',
        'LK' => 'Sri Lanka',
        'SD' => 'Sudan',
        'SR' => 'Suriname',
        'SJ' => 'Svalbard & Jan Mayen Islands',
        'SZ' => 'Swaziland',
        'SE' => 'Sweden',
        'CH' => 'Switzerland, Swiss Confederation',
        'SY' => 'Syrian Arab Republic',
        'TW' => 'Taiwan',
        'TJ' => 'Tajikistan',
        'TZ' => 'Tanzania',
        'TH' => 'Thailand',
        'TL' => 'Timor-Leste',
        'TG' => 'Togo',
        'TK' => 'Tokelau',
        'TO' => 'Tonga',
        'TT' => 'Trinidad and Tobago',
        'TN' => 'Tunisia',
        'TR' => 'Turkey',
        'TM' => 'Turkmenistan',
        'TC' => 'Turks and Caicos Islands',
        'TV' => 'Tuvalu',
        'UG' => 'Uganda',
        'UA' => 'Ukraine',
        'AE' => 'United Arab Emirates',
        'GB' => 'United Kingdom',
        'US' => 'United States of America',
        'UM' => 'United States Minor Outlying Islands',
        'VI' => 'United States Virgin Islands',
        'UY' => 'Uruguay, Eastern Republic of',
        'UZ' => 'Uzbekistan',
        'VU' => 'Vanuatu',
        'VE' => 'Venezuela',
        'VN' => 'Vietnam',
        'WF' => 'Wallis and Futuna',
        'EH' => 'Western Sahara',
        'YE' => 'Yemen',
        'ZM' => 'Zambia',
        'ZW' => 'Zimbabwe'
    );

    if( !$countryList[$code] ) return $code;
    else return $countryList[$code];
    }
于 2015-08-02T19:26:10.743 回答
24

您可以在http://country.io/names.json获得 ISO2 国家代码到名称的 JSON 映射。这是一个使用它的简单 PHP 示例:

$names = json_decode(file_get_contents("http://country.io/names.json"), true);
echo $names['US'];
// => United States
echo $names['NL'];
// => Netherlands

有关更多相关数据,请参见http://country.io/data/

于 2014-12-20T16:17:01.283 回答
2

这是你想要的功能:

function countryCodeToCountry($code) {
    $code = strtoupper($code);
    if ($code == 'AF') return 'Afghanistan';
    if ($code == 'AX') return 'Aland Islands';
    if ($code == 'AL') return 'Albania';
    if ($code == 'DZ') return 'Algeria';
    if ($code == 'AS') return 'American Samoa';
    if ($code == 'AD') return 'Andorra';
    if ($code == 'AO') return 'Angola';
    if ($code == 'AI') return 'Anguilla';
    if ($code == 'AQ') return 'Antarctica';
    if ($code == 'AG') return 'Antigua and Barbuda';
    if ($code == 'AR') return 'Argentina';
    if ($code == 'AM') return 'Armenia';
    if ($code == 'AW') return 'Aruba';
    if ($code == 'AU') return 'Australia';
    if ($code == 'AT') return 'Austria';
    if ($code == 'AZ') return 'Azerbaijan';
    if ($code == 'BS') return 'Bahamas the';
    if ($code == 'BH') return 'Bahrain';
    if ($code == 'BD') return 'Bangladesh';
    if ($code == 'BB') return 'Barbados';
    if ($code == 'BY') return 'Belarus';
    if ($code == 'BE') return 'Belgium';
    if ($code == 'BZ') return 'Belize';
    if ($code == 'BJ') return 'Benin';
    if ($code == 'BM') return 'Bermuda';
    if ($code == 'BT') return 'Bhutan';
    if ($code == 'BO') return 'Bolivia';
    if ($code == 'BA') return 'Bosnia and Herzegovina';
    if ($code == 'BW') return 'Botswana';
    if ($code == 'BV') return 'Bouvet Island (Bouvetoya)';
    if ($code == 'BR') return 'Brazil';
    if ($code == 'IO') return 'British Indian Ocean Territory (Chagos Archipelago)';
    if ($code == 'VG') return 'British Virgin Islands';
    if ($code == 'BN') return 'Brunei Darussalam';
    if ($code == 'BG') return 'Bulgaria';
    if ($code == 'BF') return 'Burkina Faso';
    if ($code == 'BI') return 'Burundi';
    if ($code == 'KH') return 'Cambodia';
    if ($code == 'CM') return 'Cameroon';
    if ($code == 'CA') return 'Canada';
    if ($code == 'CV') return 'Cape Verde';
    if ($code == 'KY') return 'Cayman Islands';
    if ($code == 'CF') return 'Central African Republic';
    if ($code == 'TD') return 'Chad';
    if ($code == 'CL') return 'Chile';
    if ($code == 'CN') return 'China';
    if ($code == 'CX') return 'Christmas Island';
    if ($code == 'CC') return 'Cocos (Keeling) Islands';
    if ($code == 'CO') return 'Colombia';
    if ($code == 'KM') return 'Comoros the';
    if ($code == 'CD') return 'Congo';
    if ($code == 'CG') return 'Congo the';
    if ($code == 'CK') return 'Cook Islands';
    if ($code == 'CR') return 'Costa Rica';
    if ($code == 'CI') return 'Cote d\'Ivoire';
    if ($code == 'HR') return 'Croatia';
    if ($code == 'CU') return 'Cuba';
    if ($code == 'CY') return 'Cyprus';
    if ($code == 'CZ') return 'Czech Republic';
    if ($code == 'DK') return 'Denmark';
    if ($code == 'DJ') return 'Djibouti';
    if ($code == 'DM') return 'Dominica';
    if ($code == 'DO') return 'Dominican Republic';
    if ($code == 'EC') return 'Ecuador';
    if ($code == 'EG') return 'Egypt';
    if ($code == 'SV') return 'El Salvador';
    if ($code == 'GQ') return 'Equatorial Guinea';
    if ($code == 'ER') return 'Eritrea';
    if ($code == 'EE') return 'Estonia';
    if ($code == 'ET') return 'Ethiopia';
    if ($code == 'FO') return 'Faroe Islands';
    if ($code == 'FK') return 'Falkland Islands (Malvinas)';
    if ($code == 'FJ') return 'Fiji the Fiji Islands';
    if ($code == 'FI') return 'Finland';
    if ($code == 'FR') return 'France, French Republic';
    if ($code == 'GF') return 'French Guiana';
    if ($code == 'PF') return 'French Polynesia';
    if ($code == 'TF') return 'French Southern Territories';
    if ($code == 'GA') return 'Gabon';
    if ($code == 'GM') return 'Gambia the';
    if ($code == 'GE') return 'Georgia';
    if ($code == 'DE') return 'Germany';
    if ($code == 'GH') return 'Ghana';
    if ($code == 'GI') return 'Gibraltar';
    if ($code == 'GR') return 'Greece';
    if ($code == 'GL') return 'Greenland';
    if ($code == 'GD') return 'Grenada';
    if ($code == 'GP') return 'Guadeloupe';
    if ($code == 'GU') return 'Guam';
    if ($code == 'GT') return 'Guatemala';
    if ($code == 'GG') return 'Guernsey';
    if ($code == 'GN') return 'Guinea';
    if ($code == 'GW') return 'Guinea-Bissau';
    if ($code == 'GY') return 'Guyana';
    if ($code == 'HT') return 'Haiti';
    if ($code == 'HM') return 'Heard Island and McDonald Islands';
    if ($code == 'VA') return 'Holy See (Vatican City State)';
    if ($code == 'HN') return 'Honduras';
    if ($code == 'HK') return 'Hong Kong';
    if ($code == 'HU') return 'Hungary';
    if ($code == 'IS') return 'Iceland';
    if ($code == 'IN') return 'India';
    if ($code == 'ID') return 'Indonesia';
    if ($code == 'IR') return 'Iran';
    if ($code == 'IQ') return 'Iraq';
    if ($code == 'IE') return 'Ireland';
    if ($code == 'IM') return 'Isle of Man';
    if ($code == 'IL') return 'Israel';
    if ($code == 'IT') return 'Italy';
    if ($code == 'JM') return 'Jamaica';
    if ($code == 'JP') return 'Japan';
    if ($code == 'JE') return 'Jersey';
    if ($code == 'JO') return 'Jordan';
    if ($code == 'KZ') return 'Kazakhstan';
    if ($code == 'KE') return 'Kenya';
    if ($code == 'KI') return 'Kiribati';
    if ($code == 'KP') return 'Korea';
    if ($code == 'KR') return 'Korea';
    if ($code == 'KW') return 'Kuwait';
    if ($code == 'KG') return 'Kyrgyz Republic';
    if ($code == 'LA') return 'Lao';
    if ($code == 'LV') return 'Latvia';
    if ($code == 'LB') return 'Lebanon';
    if ($code == 'LS') return 'Lesotho';
    if ($code == 'LR') return 'Liberia';
    if ($code == 'LY') return 'Libyan Arab Jamahiriya';
    if ($code == 'LI') return 'Liechtenstein';
    if ($code == 'LT') return 'Lithuania';
    if ($code == 'LU') return 'Luxembourg';
    if ($code == 'MO') return 'Macao';
    if ($code == 'MK') return 'Macedonia';
    if ($code == 'MG') return 'Madagascar';
    if ($code == 'MW') return 'Malawi';
    if ($code == 'MY') return 'Malaysia';
    if ($code == 'MV') return 'Maldives';
    if ($code == 'ML') return 'Mali';
    if ($code == 'MT') return 'Malta';
    if ($code == 'MH') return 'Marshall Islands';
    if ($code == 'MQ') return 'Martinique';
    if ($code == 'MR') return 'Mauritania';
    if ($code == 'MU') return 'Mauritius';
    if ($code == 'YT') return 'Mayotte';
    if ($code == 'MX') return 'Mexico';
    if ($code == 'FM') return 'Micronesia';
    if ($code == 'MD') return 'Moldova';
    if ($code == 'MC') return 'Monaco';
    if ($code == 'MN') return 'Mongolia';
    if ($code == 'ME') return 'Montenegro';
    if ($code == 'MS') return 'Montserrat';
    if ($code == 'MA') return 'Morocco';
    if ($code == 'MZ') return 'Mozambique';
    if ($code == 'MM') return 'Myanmar';
    if ($code == 'NA') return 'Namibia';
    if ($code == 'NR') return 'Nauru';
    if ($code == 'NP') return 'Nepal';
    if ($code == 'AN') return 'Netherlands Antilles';
    if ($code == 'NL') return 'Netherlands the';
    if ($code == 'NC') return 'New Caledonia';
    if ($code == 'NZ') return 'New Zealand';
    if ($code == 'NI') return 'Nicaragua';
    if ($code == 'NE') return 'Niger';
    if ($code == 'NG') return 'Nigeria';
    if ($code == 'NU') return 'Niue';
    if ($code == 'NF') return 'Norfolk Island';
    if ($code == 'MP') return 'Northern Mariana Islands';
    if ($code == 'NO') return 'Norway';
    if ($code == 'OM') return 'Oman';
    if ($code == 'PK') return 'Pakistan';
    if ($code == 'PW') return 'Palau';
    if ($code == 'PS') return 'Palestinian Territory';
    if ($code == 'PA') return 'Panama';
    if ($code == 'PG') return 'Papua New Guinea';
    if ($code == 'PY') return 'Paraguay';
    if ($code == 'PE') return 'Peru';
    if ($code == 'PH') return 'Philippines';
    if ($code == 'PN') return 'Pitcairn Islands';
    if ($code == 'PL') return 'Poland';
    if ($code == 'PT') return 'Portugal, Portuguese Republic';
    if ($code == 'PR') return 'Puerto Rico';
    if ($code == 'QA') return 'Qatar';
    if ($code == 'RE') return 'Reunion';
    if ($code == 'RO') return 'Romania';
    if ($code == 'RU') return 'Russian Federation';
    if ($code == 'RW') return 'Rwanda';
    if ($code == 'BL') return 'Saint Barthelemy';
    if ($code == 'SH') return 'Saint Helena';
    if ($code == 'KN') return 'Saint Kitts and Nevis';
    if ($code == 'LC') return 'Saint Lucia';
    if ($code == 'MF') return 'Saint Martin';
    if ($code == 'PM') return 'Saint Pierre and Miquelon';
    if ($code == 'VC') return 'Saint Vincent and the Grenadines';
    if ($code == 'WS') return 'Samoa';
    if ($code == 'SM') return 'San Marino';
    if ($code == 'ST') return 'Sao Tome and Principe';
    if ($code == 'SA') return 'Saudi Arabia';
    if ($code == 'SN') return 'Senegal';
    if ($code == 'RS') return 'Serbia';
    if ($code == 'SC') return 'Seychelles';
    if ($code == 'SL') return 'Sierra Leone';
    if ($code == 'SG') return 'Singapore';
    if ($code == 'SK') return 'Slovakia (Slovak Republic)';
    if ($code == 'SI') return 'Slovenia';
    if ($code == 'SB') return 'Solomon Islands';
    if ($code == 'SO') return 'Somalia, Somali Republic';
    if ($code == 'ZA') return 'South Africa';
    if ($code == 'GS') return 'South Georgia and the South Sandwich Islands';
    if ($code == 'ES') return 'Spain';
    if ($code == 'LK') return 'Sri Lanka';
    if ($code == 'SD') return 'Sudan';
    if ($code == 'SR') return 'Suriname';
    if ($code == 'SJ') return 'Svalbard & Jan Mayen Islands';
    if ($code == 'SZ') return 'Swaziland';
    if ($code == 'SE') return 'Sweden';
    if ($code == 'CH') return 'Switzerland, Swiss Confederation';
    if ($code == 'SY') return 'Syrian Arab Republic';
    if ($code == 'TW') return 'Taiwan';
    if ($code == 'TJ') return 'Tajikistan';
    if ($code == 'TZ') return 'Tanzania';
    if ($code == 'TH') return 'Thailand';
    if ($code == 'TL') return 'Timor-Leste';
    if ($code == 'TG') return 'Togo';
    if ($code == 'TK') return 'Tokelau';
    if ($code == 'TO') return 'Tonga';
    if ($code == 'TT') return 'Trinidad and Tobago';
    if ($code == 'TN') return 'Tunisia';
    if ($code == 'TR') return 'Turkey';
    if ($code == 'TM') return 'Turkmenistan';
    if ($code == 'TC') return 'Turks and Caicos Islands';
    if ($code == 'TV') return 'Tuvalu';
    if ($code == 'UG') return 'Uganda';
    if ($code == 'UA') return 'Ukraine';
    if ($code == 'AE') return 'United Arab Emirates';
    if ($code == 'GB') return 'United Kingdom';
    if ($code == 'US') return 'United States of America';
    if ($code == 'UM') return 'United States Minor Outlying Islands';
    if ($code == 'VI') return 'United States Virgin Islands';
    if ($code == 'UY') return 'Uruguay, Eastern Republic of';
    if ($code == 'UZ') return 'Uzbekistan';
    if ($code == 'VU') return 'Vanuatu';
    if ($code == 'VE') return 'Venezuela';
    if ($code == 'VN') return 'Vietnam';
    if ($code == 'WF') return 'Wallis and Futuna';
    if ($code == 'EH') return 'Western Sahara';
    if ($code == 'YE') return 'Yemen';
    if ($code == 'XK') return 'Kosovo';
    if ($code == 'ZM') return 'Zambia';
    if ($code == 'ZW') return 'Zimbabwe';
    return '';
}    
于 2014-08-28T07:24:34.347 回答
1

可能有点晚了……

我写了一个小库来做这个和其他一些与 ISO 相关的事情。

https://github.com/php-components/isocodes

获得一些反馈甚至拉请求以改进该库会很棒。

use ISOCodes\ISO3166_1\Adapter\Json as ISO3166_1Adapter;

$countryAdapter = new ISO3166_1Adapter();
// Get the country with alpha-2 code 'NL'
$country = $countryAdapter->get('nl');

if (null === $country) {
    die('Country not found!');
}

// Get the french (fr) name.
echo $country->getName('fr');

结果将是Pays-Bas

您可以使用 alpha-2、alpha-3 或数字代码进行国家/地区搜索。

于 2016-10-23T12:55:32.167 回答
0
<?php

/* --------------------------------------- /
 * XX. IP and country (Market).
 * -------------------------------------- */

// Gets client's IP.
$ip = getenv("HTTP_CLIENT_IP")?:
getenv("HTTP_X_FORWARDED_FOR")?:
getenv("HTTP_X_FORWARDED")?:
getenv("HTTP_FORWARDED_FOR")?:
getenv("HTTP_FORWARDED")?:
getenv("REMOTE_ADDR");

// CURL init. Matchs gotten IP with API values and retrives location value.
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://ipinfo.io/{$ip}");
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER , true);
// json output.
$output = curl_exec($curl);
// CURL close.
curl_close($curl);

// "Country code to name" array.
$countries = array( "BD" => "Bangladesh", "BE" => "Belgium", "BF" => "Burkina Faso", "BG" => "Bulgaria", "BA" => "Bosnia and Herzegovina", "BB" => "Barbados", "WF" => "Wallis and Futuna", "BL" => "Saint Barthelemy", "BM" => "Bermuda", "BN" => "Brunei", "BO" => "Bolivia", "BH" => "Bahrain", "BI" => "Burundi", "BJ" => "Benin", "BT" => "Bhutan", "JM" => "Jamaica", "BV" => "Bouvet Island", "BW" => "Botswana", "WS" => "Samoa", "BQ" => "Bonaire, Saint Eustatius and Saba ", "BR" => "Brazil", "BS" => "Bahamas", "JE" => "Jersey", "BY" => "Belarus", "BZ" => "Belize", "RU" => "Russia", "RW" => "Rwanda", "RS" => "Serbia", "TL" => "East Timor", "RE" => "Reunion", "TM" => "Turkmenistan", "TJ" => "Tajikistan", "RO" => "Romania", "TK" => "Tokelau", "GW" => "Guinea-Bissau", "GU" => "Guam", "GT" => "Guatemala", "GS" => "South Georgia and the South Sandwich Islands", "GR" => "Greece", "GQ" => "Equatorial Guinea", "GP" => "Guadeloupe", "JP" => "Japan", "GY" => "Guyana", "GG" => "Guernsey", "GF" => "French Guiana", "GE" => "Georgia", "GD" => "Grenada", "GB" => "United Kingdom", "GA" => "Gabon", "SV" => "El Salvador", "GN" => "Guinea", "GM" => "Gambia", "GL" => "Greenland", "GI" => "Gibraltar", "GH" => "Ghana", "OM" => "Oman", "TN" => "Tunisia", "JO" => "Jordan", "HR" => "Croatia", "HT" => "Haiti", "HU" => "Hungary", "HK" => "Hong Kong", "HN" => "Honduras", "HM" => "Heard Island and McDonald Islands", "VE" => "Venezuela", "PR" => "Puerto Rico", "PS" => "Palestinian Territory", "PW" => "Palau", "PT" => "Portugal", "SJ" => "Svalbard and Jan Mayen", "PY" => "Paraguay", "IQ" => "Iraq", "PA" => "Panama", "PF" => "French Polynesia", "PG" => "Papua New Guinea", "PE" => "Peru", "PK" => "Pakistan", "PH" => "Philippines", "PN" => "Pitcairn", "PL" => "Poland", "PM" => "Saint Pierre and Miquelon", "ZM" => "Zambia", "EH" => "Western Sahara", "EE" => "Estonia", "EG" => "Egypt", "ZA" => "South Africa", "EC" => "Ecuador", "IT" => "Italy", "VN" => "Vietnam", "SB" => "Solomon Islands", "ET" => "Ethiopia", "SO" => "Somalia", "ZW" => "Zimbabwe", "SA" => "Saudi Arabia", "ES" => "Spain", "ER" => "Eritrea", "ME" => "Montenegro", "MD" => "Moldova", "MG" => "Madagascar", "MF" => "Saint Martin", "MA" => "Morocco", "MC" => "Monaco", "UZ" => "Uzbekistan", "MM" => "Myanmar", "ML" => "Mali", "MO" => "Macao", "MN" => "Mongolia", "MH" => "Marshall Islands", "MK" => "Macedonia", "MU" => "Mauritius", "MT" => "Malta", "MW" => "Malawi", "MV" => "Maldives", "MQ" => "Martinique", "MP" => "Northern Mariana Islands", "MS" => "Montserrat", "MR" => "Mauritania", "IM" => "Isle of Man", "UG" => "Uganda", "TZ" => "Tanzania", "MY" => "Malaysia", "MX" => "Mexico", "IL" => "Israel", "FR" => "France", "IO" => "British Indian Ocean Territory", "SH" => "Saint Helena", "FI" => "Finland", "FJ" => "Fiji", "FK" => "Falkland Islands", "FM" => "Micronesia", "FO" => "Faroe Islands", "NI" => "Nicaragua", "NL" => "Netherlands", "NO" => "Norway", "NA" => "Namibia", "VU" => "Vanuatu", "NC" => "New Caledonia", "NE" => "Niger", "NF" => "Norfolk Island", "NG" => "Nigeria", "NZ" => "New Zealand", "NP" => "Nepal", "NR" => "Nauru", "NU" => "Niue", "CK" => "Cook Islands", "XK" => "Kosovo", "CI" => "Ivory Coast", "CH" => "Switzerland", "CO" => "Colombia", "CN" => "China", "CM" => "Cameroon", "CL" => "Chile", "CC" => "Cocos Islands", "CA" => "Canada", "CG" => "Republic of the Congo", "CF" => "Central African Republic", "CD" => "Democratic Republic of the Congo", "CZ" => "Czech Republic", "CY" => "Cyprus", "CX" => "Christmas Island", "CR" => "Costa Rica", "CW" => "Curacao", "CV" => "Cape Verde", "CU" => "Cuba", "SZ" => "Swaziland", "SY" => "Syria", "SX" => "Sint Maarten", "KG" => "Kyrgyzstan", "KE" => "Kenya", "SS" => "South Sudan", "SR" => "Suriname", "KI" => "Kiribati", "KH" => "Cambodia", "KN" => "Saint Kitts and Nevis", "KM" => "Comoros", "ST" => "Sao Tome and Principe", "SK" => "Slovakia", "KR" => "South Korea", "SI" => "Slovenia", "KP" => "North Korea", "KW" => "Kuwait", "SN" => "Senegal", "SM" => "San Marino", "SL" => "Sierra Leone", "SC" => "Seychelles", "KZ" => "Kazakhstan", "KY" => "Cayman Islands", "SG" => "Singapore", "SE" => "Sweden", "SD" => "Sudan", "DO" => "Dominican Republic", "DM" => "Dominica", "DJ" => "Djibouti", "DK" => "Denmark", "VG" => "British Virgin Islands", "DE" => "Germany", "YE" => "Yemen", "DZ" => "Algeria", "US" => "United States", "UY" => "Uruguay", "YT" => "Mayotte", "UM" => "United States Minor Outlying Islands", "LB" => "Lebanon", "LC" => "Saint Lucia", "LA" => "Laos", "TV" => "Tuvalu", "TW" => "Taiwan", "TT" => "Trinidad and Tobago", "TR" => "Turkey", "LK" => "Sri Lanka", "LI" => "Liechtenstein", "LV" => "Latvia", "TO" => "Tonga", "LT" => "Lithuania", "LU" => "Luxembourg", "LR" => "Liberia", "LS" => "Lesotho", "TH" => "Thailand", "TF" => "French Southern Territories", "TG" => "Togo", "TD" => "Chad", "TC" => "Turks and Caicos Islands", "LY" => "Libya", "VA" => "Vatican", "VC" => "Saint Vincent and the Grenadines", "AE" => "United Arab Emirates", "AD" => "Andorra", "AG" => "Antigua and Barbuda", "AF" => "Afghanistan", "AI" => "Anguilla", "VI" => "U.S. Virgin Islands", "IS" => "Iceland", "IR" => "Iran", "AM" => "Armenia", "AL" => "Albania", "AO" => "Angola", "AQ" => "Antarctica", "AS" => "American Samoa", "AR" => "Argentina", "AU" => "Australia", "AT" => "Austria", "AW" => "Aruba", "IN" => "India", "AX" => "Aland Islands", "AZ" => "Azerbaijan", "IE" => "Ireland", "ID" => "Indonesia", "UA" => "Ukraine", "QA" => "Qatar", "MZ" => "Mozambique" );

// Assigns ISO Alpha-2 value to the variable "&country_code".
$country_code = json_decode($output, true);
// Assigns "$country_name" value after checking array.
$country_name = $countries[$country_code["country"]];

echo $country_name;

?>

100% 测试并且工作正常。在这里查看它的实际应用。我在许多项目中使用它,它的工作就像一个魅力。

于 2017-05-20T00:21:39.490 回答
-5
<?php
echo country_name("DZ");
function country_name($value){
     if($value=="AF"){return "Afghanistan";}
     elseif($value=="AX"){return "Åland Islands";}
     elseif($value=="AL"){return "Albania";}
     elseif($value=="DZ"){return "Algeria";}
     elseif($value=="AS"){return "American Samoa";}
     elseif($value=="AD"){return "Andorra";}
     elseif($value=="AO"){return "Angola";}
     elseif($value=="AI"){return "Anguilla";}
     elseif($value=="AQ"){return "Antarctica";}
     elseif($value=="AG"){return "Antigua and Barbuda";}
     elseif($value=="AR"){return "Argentina";}
     elseif($value=="AM"){return "Armenia";}
     elseif($value=="AW"){return "Aruba";}
     elseif($value=="AU"){return "Australia";}
     elseif($value=="AT"){return "Austria";}
     elseif($value=="AZ"){return "Azerbaijan";}
     elseif($value=="BS"){return "Bahamas";}
     elseif($value=="BH"){return "Bahrain";}
     elseif($value=="BD"){return "Bangladesh";}
     elseif($value=="BB"){return "Barbados";}
     elseif($value=="BY"){return "Belarus";}
     elseif($value=="BE"){return "Belgium";}
     elseif($value=="BZ"){return "Belize";}
     elseif($value=="BJ"){return "Benin";}
     elseif($value=="BM"){return "Bermuda";}
     elseif($value=="BT"){return "Bhutan";}
     elseif($value=="BO"){return "Bolivia, Plurinational State of";}
     elseif($value=="BQ"){return "Bonaire, Sint Eustatius and Saba";}
     elseif($value=="BA"){return "Bosnia and Herzegovina";}
     elseif($value=="BW"){return "Botswana";}
     elseif($value=="BV"){return "Bouvet Island";}
     elseif($value=="BR"){return "Brazil";}
     elseif($value=="IO"){return "British Indian Ocean Territory";}
     elseif($value=="BN"){return "Brunei Darussalam";}
     elseif($value=="BG"){return "Bulgaria";}
     elseif($value=="BF"){return "Burkina Faso";}
     elseif($value=="BI"){return "Burundi";}
     elseif($value=="KH"){return "Cambodia";}
     elseif($value=="CM"){return "Cameroon";}
     elseif($value=="CA"){return "Canada";}
     elseif($value=="CV"){return "Cape Verde";}
     elseif($value=="KY"){return "Cayman Islands";}
     elseif($value=="CF"){return "Central African Republic";}
     elseif($value=="TD"){return "Chad";}
     elseif($value=="CL"){return "Chile";}
     elseif($value=="CN"){return "China";}
     elseif($value=="CX"){return "Christmas Island";}
     elseif($value=="CC"){return "Cocos (Keeling) Islands";}
     elseif($value=="CO"){return "Colombia";}
     elseif($value=="KM"){return "Comoros";}
     elseif($value=="CG"){return "Congo";}
     elseif($value=="CD"){return "Congo, the Democratic Republic of the";}
     elseif($value=="CK"){return "Cook Islands";}
     elseif($value=="CR"){return "Costa Rica";}
     elseif($value=="CI"){return "Côte d'Ivoire";}
     elseif($value=="HR"){return "Croatia";}
     elseif($value=="CU"){return "Cuba";}
     elseif($value=="CW"){return "Curaçao";}
     elseif($value=="CY"){return "Cyprus";}
     elseif($value=="CZ"){return "Czech Republic";}
     elseif($value=="DK"){return "Denmark";}
     elseif($value=="DJ"){return "Djibouti";}
     elseif($value=="DM"){return "Dominica";}
     elseif($value=="DO"){return "Dominican Republic";}
     elseif($value=="EC"){return "Ecuador";}
     elseif($value=="EG"){return "Egypt";}
     elseif($value=="SV"){return "El Salvador";}
     elseif($value=="GQ"){return "Equatorial Guinea";}
     elseif($value=="ER"){return "Eritrea";}
     elseif($value=="EE"){return "Estonia";}
     elseif($value=="ET"){return "Ethiopia";}
     elseif($value=="FK"){return "Falkland Islands (Malvinas)";}
     elseif($value=="FO"){return "Faroe Islands";}
     elseif($value=="FJ"){return "Fiji";}
     elseif($value=="FI"){return "Finland";}
     elseif($value=="FR"){return "France";}
     elseif($value=="GF"){return "French Guiana";}
     elseif($value=="PF"){return "French Polynesia";}
     elseif($value=="TF"){return "French Southern Territories";}
     elseif($value=="GA"){return "Gabon";}
     elseif($value=="GM"){return "Gambia";}
     elseif($value=="GE"){return "Georgia";}
     elseif($value=="DE"){return "Germany";}
     elseif($value=="GH"){return "Ghana";}
     elseif($value=="GI"){return "Gibraltar";}
     elseif($value=="GR"){return "Greece";}
     elseif($value=="GL"){return "Greenland";}
     elseif($value=="GD"){return "Grenada";}
     elseif($value=="GP"){return "Guadeloupe";}
     elseif($value=="GU"){return "Guam";}
     elseif($value=="GT"){return "Guatemala";}
     elseif($value=="GG"){return "Guernsey";}
     elseif($value=="GN"){return "Guinea";}
     elseif($value=="GW"){return "Guinea-Bissau";}
     elseif($value=="GY"){return "Guyana";}
     elseif($value=="HT"){return "Haiti";}
     elseif($value=="HM"){return "Heard Island and McDonald Islands";}
     elseif($value=="VA"){return "Holy See (Vatican City State)";}
     elseif($value=="HN"){return "Honduras";}
     elseif($value=="HK"){return "Hong Kong";}
     elseif($value=="HU"){return "Hungary";}
     elseif($value=="IS"){return "Iceland";}
     elseif($value=="IN"){return "India";}
     elseif($value=="ID"){return "Indonesia";}
     elseif($value=="IR"){return "Iran, Islamic Republic of";}
     elseif($value=="IQ"){return "Iraq";}
     elseif($value=="IE"){return "Ireland";}
     elseif($value=="IM"){return "Isle of Man";}
     elseif($value=="IL"){return "Israel";}
     elseif($value=="IT"){return "Italy";}
     elseif($value=="JM"){return "Jamaica";}
     elseif($value=="JP"){return "Japan";}
     elseif($value=="JE"){return "Jersey";}
     elseif($value=="JO"){return "Jordan";}
     elseif($value=="KZ"){return "Kazakhstan";}
     elseif($value=="KE"){return "Kenya";}
     elseif($value=="KI"){return "Kiribati";}
     elseif($value=="KP"){return "Korea, Democratic People's Republic of";}
     elseif($value=="KR"){return "Korea, Republic of";}
     elseif($value=="KW"){return "Kuwait";}
     elseif($value=="KG"){return "Kyrgyzstan";}
     elseif($value=="LA"){return "Lao People's Democratic Republic";}
     elseif($value=="LV"){return "Latvia";}
     elseif($value=="LB"){return "Lebanon";}
     elseif($value=="LS"){return "Lesotho";}
     elseif($value=="LR"){return "Liberia";}
     elseif($value=="LY"){return "Libya";}
     elseif($value=="LI"){return "Liechtenstein";}
     elseif($value=="LT"){return "Lithuania";}
     elseif($value=="LU"){return "Luxembourg";}
     elseif($value=="MO"){return "Macao";}
     elseif($value=="MK"){return "Macedonia, the former Yugoslav Republic of";}
     elseif($value=="MG"){return "Madagascar";}
     elseif($value=="MW"){return "Malawi";}
     elseif($value=="MY"){return "Malaysia";}
     elseif($value=="MV"){return "Maldives";}
     elseif($value=="ML"){return "Mali";}
     elseif($value=="MT"){return "Malta";}
     elseif($value=="MH"){return "Marshall Islands";}
     elseif($value=="MQ"){return "Martinique";}
     elseif($value=="MR"){return "Mauritania";}
     elseif($value=="MU"){return "Mauritius";}
     elseif($value=="YT"){return "Mayotte";}
     elseif($value=="MX"){return "Mexico";}
     elseif($value=="FM"){return "Micronesia, Federated States of";}
     elseif($value=="MD"){return "Moldova, Republic of";}
     elseif($value=="MC"){return "Monaco";}
     elseif($value=="MN"){return "Mongolia";}
     elseif($value=="ME"){return "Montenegro";}
     elseif($value=="MS"){return "Montserrat";}
     elseif($value=="MA"){return "Morocco";}
     elseif($value=="MZ"){return "Mozambique";}
     elseif($value=="MM"){return "Myanmar";}
     elseif($value=="NA"){return "Namibia";}
     elseif($value=="NR"){return "Nauru";}
     elseif($value=="NP"){return "Nepal";}
     elseif($value=="NL"){return "Netherlands";}
     elseif($value=="NC"){return "New Caledonia";}
     elseif($value=="NZ"){return "New Zealand";}
     elseif($value=="NI"){return "Nicaragua";}
     elseif($value=="NE"){return "Niger";}
     elseif($value=="NG"){return "Nigeria";}
     elseif($value=="NU"){return "Niue";}
     elseif($value=="NF"){return "Norfolk Island";}
     elseif($value=="MP"){return "Northern Mariana Islands";}
     elseif($value=="NO"){return "Norway";}
     elseif($value=="OM"){return "Oman";}
     elseif($value=="PK"){return "Pakistan";}
     elseif($value=="PW"){return "Palau";}
     elseif($value=="PS"){return "Palestinian Territory, Occupied";}
     elseif($value=="PA"){return "Panama";}
     elseif($value=="PG"){return "Papua New Guinea";}
     elseif($value=="PY"){return "Paraguay";}
     elseif($value=="PE"){return "Peru";}
     elseif($value=="PH"){return "Philippines";}
     elseif($value=="PN"){return "Pitcairn";}
     elseif($value=="PL"){return "Poland";}
     elseif($value=="PT"){return "Portugal";}
     elseif($value=="PR"){return "Puerto Rico";}
     elseif($value=="QA"){return "Qatar";}
     elseif($value=="RE"){return "Réunion";}
     elseif($value=="RO"){return "Romania";}
     elseif($value=="RU"){return "Russian Federation";}
     elseif($value=="RW"){return "Rwanda";}
     elseif($value=="BL"){return "Saint Barthélemy";}
     elseif($value=="SH"){return "Saint Helena, Ascension and Tristan da Cunha";}
     elseif($value=="KN"){return "Saint Kitts and Nevis";}
     elseif($value=="LC"){return "Saint Lucia";}
     elseif($value=="MF"){return "Saint Martin (French part)";}
     elseif($value=="PM"){return "Saint Pierre and Miquelon";}
     elseif($value=="VC"){return "Saint Vincent and the Grenadines";}
     elseif($value=="WS"){return "Samoa";}
     elseif($value=="SM"){return "San Marino";}
     elseif($value=="ST"){return "Sao Tome and Principe";}
     elseif($value=="SA"){return "Saudi Arabia";}
     elseif($value=="SN"){return "Senegal";}
     elseif($value=="RS"){return "Serbia";}
     elseif($value=="SC"){return "Seychelles";}
     elseif($value=="SL"){return "Sierra Leone";}
     elseif($value=="SG"){return "Singapore";}
     elseif($value=="SX"){return "Sint Maarten (Dutch part)";}
     elseif($value=="SK"){return "Slovakia";}
     elseif($value=="SI"){return "Slovenia";}
     elseif($value=="SB"){return "Solomon Islands";}
     elseif($value=="SO"){return "Somalia";}
     elseif($value=="ZA"){return "South Africa";}
     elseif($value=="GS"){return "South Georgia and the South Sandwich Islands";}
     elseif($value=="SS"){return "South Sudan";}
     elseif($value=="ES"){return "Spain";}
     elseif($value=="LK"){return "Sri Lanka";}
     elseif($value=="SD"){return "Sudan";}
     elseif($value=="SR"){return "Suriname";}
     elseif($value=="SJ"){return "Svalbard and Jan Mayen";}
     elseif($value=="SZ"){return "Swaziland";}
     elseif($value=="SE"){return "Sweden";}
     elseif($value=="CH"){return "Switzerland";}
     elseif($value=="SY"){return "Syrian Arab Republic";}
     elseif($value=="TW"){return "Taiwan, Province of China";}
     elseif($value=="TJ"){return "Tajikistan";}
     elseif($value=="TZ"){return "Tanzania, United Republic of";}
     elseif($value=="TH"){return "Thailand";}
     elseif($value=="TL"){return "Timor-Leste";}
     elseif($value=="TG"){return "Togo";}
     elseif($value=="TK"){return "Tokelau";}
     elseif($value=="TO"){return "Tonga";}
     elseif($value=="TT"){return "Trinidad and Tobago";}
     elseif($value=="TN"){return "Tunisia";}
     elseif($value=="TR"){return "Turkey";}
     elseif($value=="TM"){return "Turkmenistan";}
     elseif($value=="TC"){return "Turks and Caicos Islands";}
     elseif($value=="TV"){return "Tuvalu";}
     elseif($value=="UG"){return "Uganda";}
     elseif($value=="UA"){return "Ukraine";}
     elseif($value=="AE"){return "United Arab Emirates";}
     elseif($value=="GB"){return "United Kingdom";}
     elseif($value=="US"){return "United States";}
     elseif($value=="UM"){return "United States Minor Outlying Islands";}
     elseif($value=="UY"){return "Uruguay";}
     elseif($value=="UZ"){return "Uzbekistan";}
     elseif($value=="VU"){return "Vanuatu";}
     elseif($value=="VE"){return "Venezuela, Bolivarian Republic of";}
     elseif($value=="VN"){return "Viet Nam";}
     elseif($value=="VG"){return "Virgin Islands, British";}
     elseif($value=="VI"){return "Virgin Islands, U.S.";}
     elseif($value=="WF"){return "Wallis and Futuna";}
     elseif($value=="EH"){return "Western Sahara";}
     elseif($value=="YE"){return "Yemen";}
     elseif($value=="ZM"){return "Zambia";}
     elseif($value=="ZW"){return "Zimbabwe";}
     else{return "Unknown Country";}
 }
 ?>
于 2019-10-18T07:18:17.403 回答