Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lat4732's avatar
Level 12

How to convert country 2 digits code to full country name

Hey!

The title is describing my question. Is there a way converting EN to England without adding any additional package?

0 likes
15 replies
lat4732's avatar
Level 12

@tykus Okay, I created a folder and a file inside resources folder. The file is named countries.php inside resources/user folder. How can I implement this file to be everywhere in the project so I can easily use it without including it manually from the controller at every page?

tykus's avatar

@Laralex actually, I that list is backwards for your needs - here is a flipped and remapped version:

How can I implement this file to be everywhere in the project so I can easily use it without including it manually from the controller at every page?

Do you have your own helpers.php file in the project - you could just make a global helper function?

[
     "US" => "United States",
     "AF" => "Afghanistan",
     "AL" => "Albania",
     "DZ" => "Algeria",
     "AS" => "American Samoa",
     "AD" => "Angola",
     "AI" => "Anguilla",
     "AQ" => "Antarctica",
     "AG" => "Antigua and Barbuda",
     "AR" => "Argentina",
     "AM" => "Armenia",
     "AW" => "Aruba",
     "AU" => "Australia",
     "AT" => "Austria",
     "AZ" => "Azerbaijan",
     "BS" => "Bahamas",
     "BH" => "Bahrain",
     "BD" => "Bangladesh",
     "BB" => "Barbados",
     "BY" => "Belarus",
     "BE" => "Belgium",
     "BZ" => "Belize",
     "BJ" => "Benin",
     "BM" => "Bermuda",
     "BT" => "Bhutan",
     "BO" => "Bolivia",
     "BA" => "Bosnia and Herzegowina",
     "BW" => "Botswana",
     "BV" => "Bouvet Island",
     "BR" => "Brazil",
     "IO" => "British Indian Ocean Territory",
     "BN" => "Brunei Darussalam",
     "BG" => "Bulgaria",
     "BF" => "Burkina Faso",
     "BI" => "Burundi",
     "KH" => "Cambodia",
     "CM" => "Cameroon",
     "CA" => "Canada",
     "CV" => "Cabo 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",
     "CG" => "Congo",
     "CD" => "Congo, the Democratic Republic of the",
     "CK" => "Cook Islands",
     "CR" => "Costa Rica",
     "CI" => "Cote d'Ivoire",
     "HR" => "Croatia (Hrvatska)",
     "CU" => "Cuba",
     "CY" => "Cyprus",
     "CZ" => "Czech Republic",
     "DK" => "Denmark",
     "DJ" => "Djibouti",
     "DM" => "Dominica",
     "DO" => "Dominican Republic",
     "TL" => "East Timor",
     "EC" => "Ecuador",
     "EG" => "Egypt",
     "SV" => "El Salvador",
     "GQ" => "Equatorial Guinea",
     "ER" => "Eritrea",
     "EE" => "Estonia",
     "ET" => "Ethiopia",
     "FK" => "Falkland Islands (Malvinas)",
     "FO" => "Faroe Islands",
     "FJ" => "Fiji",
     "FI" => "Finland",
     "FR" => "France",
     "GF" => "French Guiana",
     "PF" => "French Polynesia",
     "TF" => "French Southern Territories",
     "GA" => "Gabon",
     "GM" => "Gambia",
     "GE" => "Georgia",
     "DE" => "Germany",
     "GH" => "Ghana",
     "GI" => "Gibraltar",
     "GR" => "Greece",
     "GL" => "Greenland",
     "GD" => "Grenada",
     "GP" => "Guadeloupe",
     "GU" => "Guam",
     "GT" => "Guatemala",
     "GN" => "Guinea",
     "GW" => "Guinea-Bissau",
     "GY" => "Guyana",
     "HT" => "Haiti",
     "HM" => "Heard and Mc Donald Islands",
     "VA" => "Holy See (Vatican City State)",
     "HN" => "Honduras",
     "HK" => "Hong Kong",
     "HU" => "Hungary",
     "IS" => "Iceland",
     "IN" => "India",
     "ID" => "Indonesia",
     "IR" => "Iran (Islamic Republic of)",
     "IQ" => "Iraq",
     "IE" => "Ireland",
     "IL" => "Israel",
     "IT" => "Italy",
     "JM" => "Jamaica",
     "JP" => "Japan",
     "JO" => "Jordan",
     "KZ" => "Kazakhstan",
     "KE" => "Kenya",
     "KI" => "Kiribati",
     "KP" => "Korea, Democratic People's Republic of",
     "KR" => "Korea, Republic of",
     "KW" => "Kuwait",
     "KG" => "Kyrgyzstan",
     "LA" => "Lao, People's Democratic Republic",
     "LV" => "Latvia",
     "LB" => "Lebanon",
     "LS" => "Lesotho",
     "LR" => "Liberia",
     "LY" => "Libyan Arab Jamahiriya",
     "LI" => "Liechtenstein",
     "LT" => "Lithuania",
     "LU" => "Luxembourg",
     "MO" => "Macao",
     "MK" => "Macedonia, The Former Yugoslav Republic of",
     "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, Federated States of",
     "MD" => "Moldova, Republic of",
     "MC" => "Monaco",
     "MN" => "Mongolia",
     "MS" => "Montserrat",
     "MA" => "Morocco",
     "MZ" => "Mozambique",
     "MM" => "Myanmar",
     "NA" => "Namibia",
     "NR" => "Nauru",
     "NP" => "Nepal",
     "NL" => "Netherlands",
     "AN" => "Netherlands Antilles",
     "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",
     "PA" => "Panama",
     "PG" => "Papua New Guinea",
     "PY" => "Paraguay",
     "PE" => "Peru",
     "PH" => "Philippines",
     "PN" => "Pitcairn",
     "PL" => "Poland",
     "PT" => "Portugal",
     "PR" => "Puerto Rico",
     "QA" => "Qatar",
     "RE" => "Reunion",
     "RO" => "Romania",
     "RU" => "Russian Federation",
     "RW" => "Rwanda",
     "KN" => "Saint Kitts and Nevis",
     "LC" => "Saint Lucia",
     "VC" => "Saint Vincent and the Grenadines",
     "WS" => "Samoa",
     "SM" => "San Marino",
     "ST" => "Sao Tome and Principe",
     "SA" => "Saudi Arabia",
     "SN" => "Senegal",
     "SC" => "Seychelles",
     "SL" => "Sierra Leone",
     "SG" => "Singapore",
     "SK" => "Slovakia (Slovak Republic)",
     "SI" => "Slovenia",
     "SB" => "Solomon Islands",
     "SO" => "Somalia",
     "ZA" => "South Africa",
     "GS" => "South Georgia and the South Sandwich Islands",
     "ES" => "Spain",
     "LK" => "Sri Lanka",
     "SH" => "St. Helena",
     "PM" => "St. Pierre and Miquelon",
     "SD" => "Sudan",
     "SR" => "Suriname",
     "SJ" => "Svalbard and Jan Mayen Islands",
     "SZ" => "Swaziland",
     "SE" => "Sweden",
     "CH" => "Switzerland",
     "SY" => "Syrian Arab Republic",
     "TW" => "Taiwan, Province of China",
     "TJ" => "Tajikistan",
     "TZ" => "Tanzania, United Republic of",
     "TH" => "Thailand",
     "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",
     "UM" => "United States Minor Outlying Islands",
     "UY" => "Uruguay",
     "UZ" => "Uzbekistan",
     "VU" => "Vanuatu",
     "VE" => "Venezuela",
     "VN" => "Vietnam",
     "VG" => "Virgin Islands (British)",
     "VI" => "Virgin Islands (U.S.)",
     "WF" => "Wallis and Futuna Islands",
     "EH" => "Western Sahara",
     "YE" => "Yemen",
     "YU" => "Serbia",
     "ZM" => "Zambia",
     "ZW" => "Zimbabwe",
   ]
tykus's avatar
tykus
Best Answer
Level 104

@Laralex okay, you could do it like this:

if (!function_exists('country_name')) {
    function country_name($code) {
        return [
            'US' => 'United States',
            // ... etc
        ][$code] ?? null;
    }
}

Now, if you need the Countries list for other reasons (e.g. you want a select form field with the full list, or you want the reverse of above get code from country name), then I would consider making a file to provide just the array, and read that file into the helper function(s)

1 like
lat4732's avatar
Level 12

@tykus Okay, everything is working fine but I'm confused how can I extract this country list for example in a select list? Because I have a select list for country all around my app and I manually edit them (they're about 6 lists) when I need to make some changes which is pretty annoying...

tykus's avatar

@Laralex I did mention this:

Now, if you need the Countries list for other reasons (e.g. you want a select form field with the full list, or you want the reverse of above get code from country name), then I would consider making a file to provide just the array, and read that file into the helper function(s)

Here is the idea:

  • store the array as JSON - because it might be useful also in your javascript application
// resources/js/countries.json
{
  "US":"United States",
  "AF":"Afghanistan",
  "AL":"Albania",
  // ...
}
  • Update your helpers.php functions:
function countries_list()
{
    return json_decode(file_get_contents(resource_path('js/countries.json')), true);
}
function country_name($code) {
    return countries_list()[$code] ?? null;
}
function country_code($name) {
    return array_search($name, countries_list()) ?: null;
}
  • your select field can be populated using
<select name="country">
@foreach(countries_list() as $code => $name)
    <option value="{{ $code }}">{{ $name }}</option>
@endforeach
</select>

Here is the list as JSON:

{
    "US": "United States",
    "AF": "Afghanistan",
    "AL": "Albania",
    "DZ": "Algeria",
    "AS": "American Samoa",
    "AD": "Angola",
    "AI": "Anguilla",
    "AQ": "Antarctica",
    "AG": "Antigua and Barbuda",
    "AR": "Argentina",
    "AM": "Armenia",
    "AW": "Aruba",
    "AU": "Australia",
    "AT": "Austria",
    "AZ": "Azerbaijan",
    "BS": "Bahamas",
    "BH": "Bahrain",
    "BD": "Bangladesh",
    "BB": "Barbados",
    "BY": "Belarus",
    "BE": "Belgium",
    "BZ": "Belize",
    "BJ": "Benin",
    "BM": "Bermuda",
    "BT": "Bhutan",
    "BO": "Bolivia",
    "BA": "Bosnia and Herzegowina",
    "BW": "Botswana",
    "BV": "Bouvet Island",
    "BR": "Brazil",
    "IO": "British Indian Ocean Territory",
    "BN": "Brunei Darussalam",
    "BG": "Bulgaria",
    "BF": "Burkina Faso",
    "BI": "Burundi",
    "KH": "Cambodia",
    "CM": "Cameroon",
    "CA": "Canada",
    "CV": "Cabo 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",
    "CG": "Congo",
    "CD": "Congo, the Democratic Republic of the",
    "CK": "Cook Islands",
    "CR": "Costa Rica",
    "CI": "Cote d'Ivoire",
    "HR": "Croatia (Hrvatska)",
    "CU": "Cuba",
    "CY": "Cyprus",
    "CZ": "Czech Republic",
    "DK": "Denmark",
    "DJ": "Djibouti",
    "DM": "Dominica",
    "DO": "Dominican Republic",
    "TL": "East Timor",
    "EC": "Ecuador",
    "EG": "Egypt",
    "SV": "El Salvador",
    "GQ": "Equatorial Guinea",
    "ER": "Eritrea",
    "EE": "Estonia",
    "ET": "Ethiopia",
    "FK": "Falkland Islands (Malvinas)",
    "FO": "Faroe Islands",
    "FJ": "Fiji",
    "FI": "Finland",
    "FR": "France",
    "GF": "French Guiana",
    "PF": "French Polynesia",
    "TF": "French Southern Territories",
    "GA": "Gabon",
    "GM": "Gambia",
    "GE": "Georgia",
    "DE": "Germany",
    "GH": "Ghana",
    "GI": "Gibraltar",
    "GR": "Greece",
    "GL": "Greenland",
    "GD": "Grenada",
    "GP": "Guadeloupe",
    "GU": "Guam",
    "GT": "Guatemala",
    "GN": "Guinea",
    "GW": "Guinea-Bissau",
    "GY": "Guyana",
    "HT": "Haiti",
    "HM": "Heard and Mc Donald Islands",
    "VA": "Holy See (Vatican City State)",
    "HN": "Honduras",
    "HK": "Hong Kong",
    "HU": "Hungary",
    "IS": "Iceland",
    "IN": "India",
    "ID": "Indonesia",
    "IR": "Iran (Islamic Republic of)",
    "IQ": "Iraq",
    "IE": "Ireland",
    "IL": "Israel",
    "IT": "Italy",
    "JM": "Jamaica",
    "JP": "Japan",
    "JO": "Jordan",
    "KZ": "Kazakhstan",
    "KE": "Kenya",
    "KI": "Kiribati",
    "KP": "Korea, Democratic People's Republic of",
    "KR": "Korea, Republic of",
    "KW": "Kuwait",
    "KG": "Kyrgyzstan",
    "LA": "Lao, People's Democratic Republic",
    "LV": "Latvia",
    "LB": "Lebanon",
    "LS": "Lesotho",
    "LR": "Liberia",
    "LY": "Libyan Arab Jamahiriya",
    "LI": "Liechtenstein",
    "LT": "Lithuania",
    "LU": "Luxembourg",
    "MO": "Macao",
    "MK": "Macedonia, The Former Yugoslav Republic of",
    "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, Federated States of",
    "MD": "Moldova, Republic of",
    "MC": "Monaco",
    "MN": "Mongolia",
    "MS": "Montserrat",
    "MA": "Morocco",
    "MZ": "Mozambique",
    "MM": "Myanmar",
    "NA": "Namibia",
    "NR": "Nauru",
    "NP": "Nepal",
    "NL": "Netherlands",
    "AN": "Netherlands Antilles",
    "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",
    "PA": "Panama",
    "PG": "Papua New Guinea",
    "PY": "Paraguay",
    "PE": "Peru",
    "PH": "Philippines",
    "PN": "Pitcairn",
    "PL": "Poland",
    "PT": "Portugal",
    "PR": "Puerto Rico",
    "QA": "Qatar",
    "RE": "Reunion",
    "RO": "Romania",
    "RU": "Russian Federation",
    "RW": "Rwanda",
    "KN": "Saint Kitts and Nevis",
    "LC": "Saint Lucia",
    "VC": "Saint Vincent and the Grenadines",
    "WS": "Samoa",
    "SM": "San Marino",
    "ST": "Sao Tome and Principe",
    "SA": "Saudi Arabia",
    "SN": "Senegal",
    "SC": "Seychelles",
    "SL": "Sierra Leone",
    "SG": "Singapore",
    "SK": "Slovakia (Slovak Republic)",
    "SI": "Slovenia",
    "SB": "Solomon Islands",
    "SO": "Somalia",
    "ZA": "South Africa",
    "GS": "South Georgia and the South Sandwich Islands",
    "ES": "Spain",
    "LK": "Sri Lanka",
    "SH": "St. Helena",
    "PM": "St. Pierre and Miquelon",
    "SD": "Sudan",
    "SR": "Suriname",
    "SJ": "Svalbard and Jan Mayen Islands",
    "SZ": "Swaziland",
    "SE": "Sweden",
    "CH": "Switzerland",
    "SY": "Syrian Arab Republic",
    "TW": "Taiwan, Province of China",
    "TJ": "Tajikistan",
    "TZ": "Tanzania, United Republic of",
    "TH": "Thailand",
    "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",
    "UM": "United States Minor Outlying Islands",
    "UY": "Uruguay",
    "UZ": "Uzbekistan",
    "VU": "Vanuatu",
    "VE": "Venezuela",
    "VN": "Vietnam",
    "VG": "Virgin Islands (British)",
    "VI": "Virgin Islands (U.S.)",
    "WF": "Wallis and Futuna Islands",
    "EH": "Western Sahara",
    "YE": "Yemen",
    "YU": "Serbia",
    "ZM": "Zambia",
    "ZW": "Zimbabwe"
}
lat4732's avatar
Level 12

@tykus Hey, another question. Is that the list of countries you gave me is correct for the current year (2022)? If it's not - can you suggest any actual json list that will work for me?

tykus's avatar

@Laralex πŸ€·β€β™‚οΈ no clue - have not audited it.

lat4732's avatar
Level 12

@tykus Probably annoying for you but is there a way to make a custom rule inside request validation to check if the country code exists in the list on form submission because option value can be easily changed through "Inspect Element".. πŸ€·β€β™‚οΈ

tykus's avatar

@Laralex something like this:

'country_code' => ['required', Rule::in(array_keys(countries_list()))]
1 like
olivsinz's avatar

You can use native PHP: Locale::getDisplayRegion or locale_get_display_region

Locale::getDisplayRegion -- locale_get_display_region β€” Returns an appropriately localized display name for region of the input locale

Source: https://www.php.net/manual/en/locale.getdisplayregion.php

To get country name : You don't have to have a fully-formed locale for the first parameter. This makes the function useful for getting the country name from any locale.

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

//Returns
string 'Γ‰tats-Unis' (length=11)
?>
1 like

Please or to participate in this conversation.