Convert country name to ISO country code

Public

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

</> CopyGet raw version
php
  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/locale.inc';
  9. $countries = country_get_list();
  10. return array_search(strtolower($country), array_map('strtolower', $countries));
  11. }

Comments

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