You are here

function countries_country_lookup in Countries 7

Same name and namespace in other branches
  1. 8 countries.module \countries_country_lookup()
  2. 7.2 countries.module \countries_country_lookup()

A helper function to find a country based on any country property.

Parameters

string $value:

string $property: The country property to search with. If empty, the code will attempt to guess what property the user is looking for. This is not recommended!

1 call to countries_country_lookup()
countries_feeds_processor_targets_alter_callback in ./countries.module
Callback defined via countries_feeds_processor_targets_alter().

File

./countries.module, line 735

Code

function countries_country_lookup($value, $property = NULL) {

  // For lazy coders, try to discover the $key from the $value.
  if (empty($property)) {
    $length = drupal_strlen($value);
    switch ($length) {
      case 2:
        $property = 'iso2';
        break;
      case 3:
        $property = is_numeric($value) ? 'numcode' : 'iso3';
        break;
      default:
        $property = 'name';
    }
  }

  // Helper function to maximise the lookup chances.
  switch ($property) {
    case 'iso2':
    case 'iso3':
      $value = drupal_strtoupper($value);
      break;
    case 'numcode':
      $value = intval($value);
      break;
    default:
      $value = trim($value);
      break;
  }
  foreach (countries_get_countries($property) as $iso2 => $country_value) {
    if ($value == $country_value) {
      return countries_get_country($iso2);
    }
  }
  return FALSE;
}