Convert country name to ISO country code


This function takes country name ("Canada" or "canada") and returns its ISO country code ("CA"):

Get raw version
  1. /**
  2.  * Convert country name to ISO country code
  3.  *
  4.  * @param $country - country name. Can be of any case
  5.  */
  6. function MODULE_NAME_convert_country_name_to_country_code($country) {
  7. // Necessary for country_get_list().
  8. require_once DRUPAL_ROOT . '/includes/';
  9. $countries = country_get_list();
  10. return array_search(strtolower($country), array_map('strtolower', $countries));
  11. }


Steffen's picture

How to do the opposite. - Transforming for example en_US to USA and en_GB til Great Britain etc? - And is this function called before node-save or what?

Syed Khadeer's picture

You can achieve this in php with the following script

echo locale::getDisplayRegion(en_IN);
This will print India as result

simran's picture

australia to aus