Creating 2 Digits Country Codes ISO 3166 And Names With PHP

ISO 3166 has become one of the world’s most well known and widely used standards for coding country names. Using a code of letters and/or numbers to represent a country name can help save time and energy, and reduce the rate of error. However, ISO 3166 does not establish the names of countries, only the codes that represent them.

ISO makes the XML of alpha-2 country codes available for internal use and non-commercial purposes free of charge. Here is the XML parsing process with simplexml_load_string PHP function:

$file = file_get_contents( 'http://www.iso.org/iso/home/standards/country_codes/country_names_and_code_elements_xml.htm', true );
	$file = str_replace( 'encoding="ISO-8859-1"', 'encoding="UTF-8"', $file );
	$xml = simplexml_load_string($file);

	$array = $code = $countries = array();

	foreach( $xml as $key => $value ) {
		$array[] = (array) $value;
		foreach( $array as $k => $v ) {
			$code[$k] = array_values( $v );
			foreach( $code[$k] as $c ) {
				$countries[$code[$k][1]] = ucwords( strtolower( utf8_decode ($code[$k][0] )) );
			}
		}
	}

	print_r( $countries );

For you who want to use the country code ISO 3166 PHP array structure:

$countries = array( 'AF' => 'Afghanistan', 'AX' => 'Åland 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', 'BH' => 'Bahrain', 'BD' => 'Bangladesh', 'BB' => 'Barbados', 'BY' => 'Belarus', 'BE' => 'Belgium', 'BZ' => 'Belize', 'BJ' => 'Benin', 'BM' => 'Bermuda', 'BT' => 'Bhutan', 'BO' => 'Bolivia, Plurinational State Of', 'BQ' => 'Bonaire, Sint Eustatius And Saba', 'BA' => 'Bosnia And Herzegovina', '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' => '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', 'CG' => 'Congo', 'CD' => 'Congo, The Democratic Republic Of The', 'CK' => 'Cook Islands', 'CR' => 'Costa Rica', 'CI' => 'Côte D\'ivoire', 'HR' => 'Croatia', 'CU' => 'Cuba', 'CW' => 'Curaçao', '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', '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', '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, Islamic Republic Of', '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, 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' => 'Libya', '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', 'ME' => 'Montenegro', 'MS' => 'Montserrat', 'MA' => 'Morocco', 'MZ' => 'Mozambique', 'MM' => 'Myanmar', 'NA' => 'Namibia', 'NR' => 'Nauru', 'NP' => 'Nepal', 'NL' => 'Netherlands', '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, Occupied', 'PA' => 'Panama', 'PG' => 'Papua New Guinea', 'PY' => 'Paraguay', 'PE' => 'Peru', 'PH' => 'Philippines', 'PN' => 'Pitcairn', 'PL' => 'Poland', 'PT' => 'Portugal', 'PR' => 'Puerto Rico', 'QA' => 'Qatar', 'RE' => 'Réunion', 'RO' => 'Romania', 'RU' => 'Russian Federation', 'RW' => 'Rwanda', 'BL' => 'Saint Barthélemy', 'SH' => 'Saint Helena, Ascension And Tristan Da Cunha', 'KN' => 'Saint Kitts And Nevis', 'LC' => 'Saint Lucia', 'MF' => 'Saint Martin (french Part)', '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', 'SX' => 'Sint Maarten (dutch Part)', 'SK' => 'Slovakia', 'SI' => 'Slovenia', 'SB' => 'Solomon Islands', 'SO' => 'Somalia', 'ZA' => 'South Africa', 'GS' => 'South Georgia And The South Sandwich Islands', 'SS' => 'South Sudan', 'ES' => 'Spain', 'LK' => 'Sri Lanka', 'SD' => 'Sudan', 'SR' => 'Suriname', 'SJ' => 'Svalbard And Jan Mayen', 'SZ' => 'Swaziland', 'SE' => 'Sweden', 'CH' => 'Switzerland', 'SY' => 'Syrian Arab Republic', 'TW' => 'Taiwan, Province Of China', 'TJ' => 'Tajikistan', 'TZ' => 'Tanzania, United Republic Of', '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', 'UM' => 'United States Minor Outlying Islands', 'UY' => 'Uruguay', 'UZ' => 'Uzbekistan', 'VU' => 'Vanuatu', 'VE' => 'Venezuela, Bolivarian Republic Of', 'VN' => 'Viet Nam', 'VG' => 'Virgin Islands, British', 'VI' => 'Virgin Islands, U.s.', 'WF' => 'Wallis And Futuna', 'EH' => 'Western Sahara', 'YE' => 'Yemen', 'ZM' => 'Zambia', 'ZW' => 'Zimbabwe' );

Country code ISO 3166 JSON format:

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

Credits:
http://en.wikipedia.org/wiki/File:Europe_ISO_3166-1.svg

Create Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Back to Top