Convert State and Country Abbreviations to Names Function

The location_name function below takes a country or a state abbrevation and returns the full name. This way, you can store "MA" and "US" (4 characters) in your database instead of "Massachusetts" and "United States" (26 characters), but still display the long version.

Before you use these, you'll need to grab a copy of our countries, states, provinces, and oceans MySQL dump and import them to your database. You'll also need to download and include our database helper functions.

You'll likely notice that two additional functions are in there too: location_slug and get_location_by_slug. The first goes from an abbreviation to a URL-friendly slug such as "west-virginia". The second goes the other direction. The slugs are simply lower case versions of the names with spaces converted to dashes. Special characters are also converted so, for example, "Querétaro" becomes "queretaro". The slugs are included in all three of the countries, states, and oceans data tables.

<?php

// Convert State and Country Abbreviations to Names Function
// https://inkplant.com/code/state-abv-to-names
// Copyright 2016.
// This version last updated Feb. 25, 2016.

function location_name($country,$state=false) {
	if ((!array_key_exists('location_names',$GLOBALS)) || (!is_array($GLOBALS['location_names']))) { $GLOBALS['location_names'] = array(); }
	if ((!array_key_exists('location_slugs',$GLOBALS)) || (!is_array($GLOBALS['location_slugs']))) { $GLOBALS['location_slugs'] = array(); }

	$key = $country;
	if ($state) { $key .= '-'.$state; }

	if ($key == 'F') { //in flight
		return '<em>In flight</em>';
	} elseif (array_key_exists($key,$GLOBALS['location_names'])) { 
		return $GLOBALS['location_names'][$key];
	}

	if ($country == 'O') { //Ocean
		if (!$state) { return '<em>International Waters</em>'; }
		$row = dbGetRow('SELECT * FROM `geo_oceans` WHERE `abv` = '.dbPrepare($state));
		$GLOBALS['location_names'][$key] = $row['name'];
		$GLOBALS['location_slugs'][$key] = $row['slug'];
		return $row['name'];
	} elseif ($state) {
		$row = dbGetRow('SELECT * FROM `geo_states` WHERE `abv` = '.dbPrepare($state).' AND `country` = '.dbPrepare($country));
		$GLOBALS['location_names'][$key] = $row['name'];
		$GLOBALS['location_slugs'][$key] = $row['slug'];
		return $row['name'];
	} else {
		$row = dbGetRow('SELECT * FROM `geo_countries` WHERE `abv` = '.dbPrepare($country));
		$GLOBALS['location_names'][$key] = $row['name'];
		$GLOBALS['location_slugs'][$key] = $row['slug'];
		return $row['name'];
	}
}

function location_slug($country,$state=false) {
	if ((!array_key_exists('location_names',$GLOBALS)) || (!is_array($GLOBALS['location_names']))) { $GLOBALS['location_names'] = array(); }
	if ((!array_key_exists('location_slugs',$GLOBALS)) || (!is_array($GLOBALS['location_slugs']))) { $GLOBALS['location_slugs'] = array(); }

	$key = $country;
	if ($state) { $key .= '-'.$state; }
	if (array_key_exists($key,$GLOBALS['location_slugs'])) { return $GLOBALS['location_slugs'][$key]; }

	if ($country == 'O') { //Ocean
		$row = dbGetRow('SELECT * FROM `geo_oceans` WHERE `abv` = '.dbPrepare($state));
		$GLOBALS['location_names'][$key] = $row['name'];
		$GLOBALS['location_slugs'][$key] = $row['slug'];
		return $row['slug'];
	} elseif ($state) {
		$row = dbGetRow('SELECT * FROM `geo_states` WHERE `abv` = '.dbPrepare($state).' AND `country` = '.dbPrepare($country));
		$GLOBALS['location_names'][$key] = $row['name'];
		$GLOBALS['location_slugs'][$key] = $row['slug'];
		return $row['slug'];
	} else {
		$row = dbGetRow('SELECT * FROM `geo_countries` WHERE `abv` = '.dbPrepare($country));
		$GLOBALS['location_names'][$key] = $row['name'];
		$GLOBALS['location_slugs'][$key] = $row['slug'];
		return $row['slug'];
	}
}

function get_location_by_slug($slug) {
	$slug = trim(strip_tags($slug));
	if (!$slug) { return array('state'=>false,'country'=>false); }
	$row = dbGetRow('SELECT * FROM `geo_states` WHERE `slug` = '.dbPrepare($slug));
	if ($row['abv']) { $state = $row['abv']; $country = $row['country']; }
	else {
		$row = dbGetRow('SELECT * FROM `geo_countries` WHERE `slug` = '.dbPrepare($slug));
		if ($row['abv']) { $state = false; $country = $row['abv']; }
		else {
			$row = dbGetRow('SELECT * FROM `geo_oceans` WHERE `slug` = '.dbPrepare($slug));
			if ($row['abv']) { $state = $row['abv']; $country = 'O'; }
			else { $state = false; $country = false; }
		}
	}
	return array('state'=>$state,'country'=>$country);
}

?>

Comments

Loading…

This post was first published on November 1st, 2014 and last updated on February 25th, 2016 by Robert James Reese in the following categories: MySQL and PHP. Before using any of the code or other content in this post, you must read and agree to our terms of use.