Created
January 12, 2016 22:02
-
-
Save jazinheira/5edf5e5b0146353999c8 to your computer and use it in GitHub Desktop.
This PHP function will take a string of a country name and return the 2 letter country code found at https://www.iso.org/obp/ui/#search.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function convert_country2code($name) { | |
$name_lc = mb_strtolower($name, 'UTF-8'); | |
$COUNTRIES = array ( | |
"andorra" => "AD", | |
"united arab emirates" => "AE", | |
"uae" => "AE", | |
"afghanista" => "AF", | |
"antigua and barbuda" => "AG", | |
"antigua" => "AG", | |
"barbuda" => "AG", | |
"anguilla" => "AI", | |
"albania" => "AL", | |
"armenia" => "AM", | |
"netherlands antilles" => "AN", | |
"angola" => "AO", | |
"antarctica" => "AQ", | |
"argentina" => "AR", | |
"american samoa" => "AS", | |
"austria" => "AT", | |
"australia" => "AU", | |
"aruba" => "AW", | |
"Åland islands" => "AX", | |
"�land islands" => "AX", //strtolower seems to handle the å oddly | |
"åland islands" => "AX", | |
"aland islands" => "AX", | |
"azerbaijan" => "AZ", | |
"bosnia" => "BA", | |
"herzegovina" => "BA", | |
"bosnia and herzegovina" => "BA", | |
"barbados" => "BB", | |
"bangladesh" => "BD", | |
"belgium" => "BE", | |
"burkina faso" => "BF", | |
"bulgaria" => "BG", | |
"bahrain" => "BH", | |
"burundi" => "BI", | |
"benin" => "BJ", | |
"saint barthélemy" => "BL", | |
"saint barthelemy" => "BL", | |
"bermuda" => "BM", | |
"brunei darussalam" => "BN", | |
"bolivia" => "BO", | |
"bolivia (plurinational state of)" => "BO", | |
"british antarctic territory" => "BQ", | |
"bonaire" => "BQ", | |
"sint eustatius" => "BQ", | |
"saba" => "BQ", | |
"bonaire, sint eurstatius and saba" => "BQ", | |
"brazil" => "BR", | |
"brasil" => "BR", | |
"bahamas" => "BS", | |
"bahamas (the)" => "BS", | |
"the bahamas" => "BS", | |
"bahamas, the" => "BS", | |
"bhutan" => "BT", | |
"burma" => "BU", | |
"bouvet island" => "BV", | |
"botswana" => "BW", | |
"byelorussian ssr" => "BY", | |
"belarus" => "BY", | |
"belize" => "BZ", | |
"canada" => "CA", | |
"cocos islands" => "CC", | |
"cocos islands, the" => "CC", | |
"keeling islands" => "CC", | |
"keeling islands, the" => "CC", | |
"cocos (keeling) islands (the)" => "CC", | |
"congo" => "CD", | |
"congo (the democratic republic of the)" => "CD", | |
"congo, the democratic republic of the" => "CD", | |
"switzerland" => "CH", | |
"côte d'ivoire" => "CI", | |
"cote d'ivoire" => "CI", | |
"cote divoire" => "CI", | |
"cook islands" => "CK", | |
"cook islands, the" => "CK", | |
"cook islands (the)" => "CK", | |
"chile" => "CL", | |
"cameroon" => "CM", | |
"china" => "CN", | |
"china, people's republic of" => "CN", | |
"colombia" => "CO", | |
"costa rica" => "CR", | |
"czechoslovakia" => "CS", | |
"serbia" => "CS", | |
"montenegro" => "ME", | |
"serbia and montenegro" => "CS", | |
"canton islands" => "CT", | |
"enderbury islands" => "CT", | |
"canton and enderbury islands" => "CT", | |
"cuba" => "CU", | |
"cabo verde" => "CV", | |
"curaçao" => "CW", | |
"curacao" => "CW", | |
"christmas island" => "CX", | |
"cyprus" => "CY", | |
"czech republic (the)" => "CZ", | |
"czech republic, the" => "CZ", | |
"the chzech republic" => "CZ", | |
"czech republic" => "CZ", | |
"german democratic republic" => "DD", | |
"germany" => "DE", | |
"djibouti" => "DJ", | |
"denmark" => "DK", | |
"dominica" => "DM", | |
"dominican republic (the)" => "DO", | |
"dominican republic, the" => "DO", | |
"the dominican republic" => "DO", | |
"dominican republic" => "DO", | |
"dahomey" => "DY", | |
"algeria" => "DZ", | |
"ecuador" => "EC", | |
"estonia" => "EE", | |
"egypt" => "EG", | |
"western sahara" => "EH", | |
"eritrea" => "ER", | |
"spain" => "ES", | |
"ethiopia" => "ET", | |
"finland" => "FI", | |
"fiji" => "FJ", | |
"falkland islands" => "FK", | |
"the falkland islands" => "FK", | |
"falkland islands (the)" => "FK", | |
"falkland islands, the" => "FK", | |
"falkland islands, the (malvinas)" => "FK", | |
"falkland islands (malvinas)" => "FK", | |
"micronesia" => "FM", | |
"micronesia, federated states of" => "FM", | |
"micronesia (federated states of" => "FM", | |
"federated states of micronesia" => "FM", | |
"faroe islands (the)" => "FO", | |
"faroe islands, the" => "FO", | |
"the faroe islands" => "FO", | |
"faroe islands" => "FO", | |
"french southern and antartic territories" => "FQ", | |
"france" => "FR", | |
"france, metropolitan" => "FX", | |
"gabon" => "GA", | |
"united kingdom" => "GB", | |
"united kingdom of great britain" => "GB", | |
"united kingdom of great britain and northern ireland" => "GB", | |
"united kingdom of great britain and northern ireland (the)" => "GB", | |
"united kingdom of great britain and northern ireland, the" => "GB", | |
"the united kingdom of great britain and northern ireland" => "GB", | |
"grenada" => "GD", | |
"gilbert island" => "GE", | |
"ellice island" => "GE", | |
"gilbert and ellice islands" => "GE", | |
"georgia" => "GE", | |
"french guiana" => "GF", | |
"guernsey" => "GG", | |
"ghana" => "GH", | |
"gibraltar" => "GI", | |
"greenland" => "GL", | |
"gambia" => "GM", | |
"gambia (the)" => "GM", | |
"gambia, the" => "GM", | |
"guinea" => "GN", | |
"guadaloupe" => "GP", | |
"equatorial guinea" => "GQ", | |
"greece" => "GR", | |
"south georgia and the south sandwich islands" => "GS", | |
"south sandwich islands" => "GS", | |
"guatemala" => "GT", | |
"guam" => "GU", | |
"guinea-bissau" => "GW", | |
"guyana" => "GY", | |
"hong kong" => "HK", | |
"heard island and mcdonald islands" => "HM", | |
"honduras" => "HN", | |
"croatia" => "HR", | |
"haiti" => "HT", | |
"hungary" => "HU", | |
"upper volta" => "HV", | |
"indonesia" => "ID", | |
"ireland" => "IE", | |
"israel" => "IL", | |
"isle of man" => "IM", | |
"india" => "IN", | |
"british indian ocean territory" => "IO", | |
"british indian ocean territory (the)" => "IO", | |
"british indian ocean territory, the" => "IO", | |
"iraq" => "IQ", | |
"iran (islamic republic of)" => "IR", | |
"iran, islamic republic of" => "IR", | |
"iran" => "IR", | |
"iceland" => "IS", | |
"italy" => "IT", | |
"jersey" => "JE", | |
"jamaica" => "JM", | |
"jordan" => "JO", | |
"japan" => "JP", | |
"johnston island" => "JT", | |
"kenya" => "KE", | |
"kyrgyzstan" => "KG", | |
"cambodia" => "KH", | |
"kiribati" => "KI", | |
"comoros (the)" => "KM", | |
"comoros, the" => "KM", | |
"comoros" => "KM", | |
"saint kitts" => "KN", | |
"nevis" => "KN", | |
"saint kitts and nevis" => "KN", | |
"korea (the democratic people's republic of)" => "KP", | |
"korea, the democratic people's republic of" => "KP", | |
"korea, democratic people's republic of" => "KP", | |
"korea, democratic people's republic" => "KP", | |
"korea" => "KR", | |
"korea (the republic of)" => "KR", | |
"korea, the republic of" => "KR", | |
"korea, republic of" => "KR", | |
"korea, (republic of)" => "KR", | |
"kuwait" => "KW", | |
"cayman islands" => "KY", | |
"cayman islands (the)" => "KY", | |
"cayman islands, the" => "KY", | |
"the cayman islands" => "KY", | |
"kazakhstan" => "KZ", | |
"lao people's democratic republic" => "LA", | |
"lao people's democratic republic (the)" => "LA", | |
"lao people's democratic republic, the" => "LA", | |
"the lao people's democratic republic" => "LA", | |
"lebanon" => "LB", | |
"saint lucia" => "LC", | |
"liechtenstein" => "LI", | |
"sri lanka" => "LK", | |
"liberia" => "LR", | |
"lesotho" => "LS", | |
"lithuania" => "LT", | |
"luxembourg" => "LU", | |
"latvia" => "LV", | |
"libya" => "LY", | |
"morocco" => "MA", | |
"monaco" => "MC", | |
"moldova" => "MD", | |
"moldova (the republic of)" => "MD", | |
"moldova, the republic of" => "MD", | |
"moldova, (republic of)" => "MD", | |
"moldova, republic of" => "MD", | |
"saint martin" => "MF", | |
"madagascar" => "MG", | |
"marshall islands" => "MH", | |
"marshall islands (the)" => "MH", | |
"marshall islands, the" => "MH", | |
"midway islands" => "MI", | |
"macedonia" => "MK", | |
"macedonia (the former yugoslav republic of)" => "MK", | |
"macedonia (former yugoslav republic of)" => "MK", | |
"macedonia (former yugoslav republic)" => "MK", | |
"macedonia, the former yugoslav republic of" => "MK", | |
"macedonia, the former yugoslav republic" => "MK", | |
"macedonia, former yugoslav republic of" => "MK", | |
"macedonia, former yugoslav republic" => "MK", | |
"mali" => "ML", | |
"myanmar" => "MM", | |
"mongolia" => "MN", | |
"macao" => "MO", | |
"northern mariana islands" => "MP", | |
"northern mariana islands (the)" => "MP", | |
"northern mariana islands, the" => "MP", | |
"martinique" => "MQ", | |
"mauritania" => "MR", | |
"montserrat" => "MS", | |
"malta" => "MT", | |
"mauritius" => "MU", | |
"maldives" => "MV", | |
"malawi" => "MW", | |
"mexico" => "MX", | |
"malaysia" => "MY", | |
"mozambique" => "MZ", | |
"namibia" => "NA", | |
"new caledonia" => "NC", | |
"niger" => "NE", | |
"niger (the)" => "NE", | |
"niger, the" => "NE", | |
"norfolk island" => "NF", | |
"nigeria" => "NG", | |
"new hebrides" => "NH", | |
"nicaragua" => "NI", | |
"netherlands" => "NL", | |
"netherlands (the)" => "NL", | |
"netherlands, the" => "NL", | |
"the netherlands" => "NL", | |
"norway" => "NO", | |
"nepal" => "NP", | |
"dronning maud land" => "NQ", | |
"nauru" => "NR", | |
"neutral zone" => "NT", | |
"niue" => "NU", | |
"new zealand" => "NZ", | |
"oman" => "OM", | |
"panama" => "PA", | |
"pacific islands" => "PC", | |
"pacific islands (trust territory)" => "PC", | |
"pacific islands, trust territory" => "PC", | |
"peru" => "PE", | |
"french polynesia" => "PF", | |
"papua new guinea" => "PG", | |
"philippines (the)" => "PH", | |
"philippines, the" => "PH", | |
"philippines" => "PH", | |
"pakistan" => "PK", | |
"poland" => "PL", | |
"saint pierre and miquelon" => "PM", | |
"pitcairn" => "PN", | |
"puerto rico" => "PR", | |
"palestine" => "PS", | |
"palestine, state of" => "PS", | |
"united states miscellaneous pacific islands" => "PU", | |
"palau" => "PW", | |
"paraguay" => "PY", | |
"panama canal zone" => "PZ", | |
"panama" => "PZ", | |
"qatar" => "QA", | |
"réunion" => "RE", | |
"reunion" => "RE", | |
"southern rhodesia" => "RH", | |
"romania" => "RO", | |
"serbia" => "RS", | |
"russia" => "RU", | |
"russian federation" => "RU", | |
"russian federation, the" => "RU", | |
"russian federation (the)" => "RU", | |
"rwanda" => "RW", | |
"saudi arabia" => "SA", | |
"solomon islands" => "SB", | |
"seychelles" => "SC", | |
"sudan" => "SD", | |
"sudan (the)" => "SD", | |
"sudan, the" => "SD", | |
"sweden" => "SE", | |
"singapore" => "SG", | |
"saint helena" => "SH", | |
"saint helenda, ascension and tristan da cunha" => "SH", | |
"slovenia" => "SI", | |
"svalbard" => "SJ", | |
"svalbard and jan mayen" => "SJ", | |
"slovakia" => "SK", | |
"sikkim" => "SK", | |
"sierra leone" => "SL", | |
"san marino" => "SM", | |
"senegal" => "SN", | |
"somalia" => "SO", | |
"suriname" => "SR", | |
"south sudan" => "SS", | |
"sao tome and principe" => "ST", | |
"ussr" => "SU", | |
"el salvador" => "SV", | |
"sint maarten" => "SX", | |
"syrian arab republic" => "SY", | |
"syria" => "SY", | |
"swaziland" => "SZ", | |
"turks and caicos islands" => "TC", | |
"turks and caicos islands (the)" => "TC", | |
"turks and caicos islands, the" => "TC", | |
"chad" => "TD", | |
"french southern territories" => "TF", | |
"french southern territories (the)" => "TF", | |
"french southern territories, the" => "TF", | |
"togo" => "TG", | |
"thailand" => "TH", | |
"tajikistan" => "TJ", | |
"tokelau" => "TK", | |
"timor-leste" => "TL", | |
"turkmenistan" => "TM", | |
"tunisia" => "TN", | |
"tonga" => "TO", | |
"east timor" => "TP", | |
"turkey" => "TR", | |
"trinidad and tobago" => "TT", | |
"tuvalu" => "TV", | |
"taiwan" => "TW", | |
"taiwan (province of china)" => "TW", | |
"taiwan, province of china" => "TW", | |
"tanzania" => "TZ", | |
"tanzania, united republic of" => "TZ", | |
"ukraine" => "UA", | |
"uganda" => "UG", | |
"united states minor outlying islands" => "UM", | |
"united states" => "US", | |
"united states of america" => "US", | |
"united states of america, the" => "US", | |
"united states of america (the)" => "US", | |
"uruguay" => "UY", | |
"uzbekistan" => "UZ", | |
"holy see" => "VA", | |
"holy see (the)" => "VA", | |
"holy see, the" => "VA", | |
"saint vincent" => "VC", | |
"grenadines" => "VC", | |
"saint vincent and the grenadines" => "VC", | |
"viet-nam, democratic republic of" => "VD", | |
"vietnam" => "VN", | |
"viet nam" => "VN", | |
"viet-nam" => "VN", | |
"vietnam, socialist republic" => "VN", | |
"vietnam, socialist republic of" => "VN", | |
"vietnam (socialist republic)" => "VN", | |
"vietnam (socialist republic of)" => "VN", | |
"viet nam, socialist republic" => "VN", | |
"venezuela" => "VE", | |
"venezuela (bolivarian republic of)" => "VE", | |
"venezuela (bolivarian republic)" => "VE", | |
"venezuela, bolvarian republic of" => "VE", | |
"venezuela, bolvarian republic" => "VE", | |
"virgin islands (british)" => "VI", | |
"virgin islands, british" => "VI", | |
"virgin islands (u.s.)" => "VI", | |
"virgin islands (us)" => "VI", | |
"virgin islands, u.s." => "VI", | |
"virgin islands, us" => "VI", | |
"vanuatu" => "VU", | |
"wallis and futuna" => "WF", | |
"wake island" => "WK", | |
"samoa" => "WS", | |
"yemen, democratic" => "YD", | |
"yemen" => "YE", | |
"mayotte" => "YT", | |
"yugoslavia" => "YU", | |
"south africa" => "ZA", | |
"zambia" => "ZM", | |
"zaire" => "ZR", | |
"zimbabwe" => "ZW" | |
); | |
if (isset($COUNTRIES[$name_lc])) { | |
return $COUNTRIES[$name_lc]; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment