Converting US State names into their Abbreviations (and back!)

May 24th, 2008 by Sean Leave a reply »

I’ve just had to find a way to convert a number of US state names into their abbreviations to make a clients life much easier so I thought I might share so that others wouldn’t have to go through the hassle I did.

First things first, here’s some PHP code…

function convert_state($name, $to='name') {
	$states = array(
	array('name'=>'Alabama', 'abbrev'=>'AL'),
	array('name'=>'Alaska', 'abbrev'=>'AK'),
	array('name'=>'Arizona', 'abbrev'=>'AZ'),
	array('name'=>'Arkansas', 'abbrev'=>'AR'),
	array('name'=>'California', 'abbrev'=>'CA'),
	array('name'=>'Colorado', 'abbrev'=>'CO'),
	array('name'=>'Connecticut', 'abbrev'=>'CT'),
	array('name'=>'Delaware', 'abbrev'=>'DE'),
	array('name'=>'Florida', 'abbrev'=>'FL'),
	array('name'=>'Georgia', 'abbrev'=>'GA'),
	array('name'=>'Hawaii', 'abbrev'=>'HI'),
	array('name'=>'Idaho', 'abbrev'=>'ID'),
	array('name'=>'Illinois', 'abbrev'=>'IL'),
	array('name'=>'Indiana', 'abbrev'=>'IN'),
	array('name'=>'Iowa', 'abbrev'=>'IA'),
	array('name'=>'Kansas', 'abbrev'=>'KS'),
	array('name'=>'Kentucky', 'abbrev'=>'KY'),
	array('name'=>'Louisiana', 'abbrev'=>'LA'),
	array('name'=>'Maine', 'abbrev'=>'ME'),
	array('name'=>'Maryland', 'abbrev'=>'MD'),
	array('name'=>'Massachusetts', 'abbrev'=>'MA'),
	array('name'=>'Michigan', 'abbrev'=>'MI'),
	array('name'=>'Minnesota', 'abbrev'=>'MN'),
	array('name'=>'Mississippi', 'abbrev'=>'MS'),
	array('name'=>'Missouri', 'abbrev'=>'MO'),
	array('name'=>'Montana', 'abbrev'=>'MT'),
	array('name'=>'Nebraska', 'abbrev'=>'NE'),
	array('name'=>'Nevada', 'abbrev'=>'NV'),
	array('name'=>'New Hampshire', 'abbrev'=>'NH'),
	array('name'=>'New Jersey', 'abbrev'=>'NJ'),
	array('name'=>'New Mexico', 'abbrev'=>'NM'),
	array('name'=>'New York', 'abbrev'=>'NY'),
	array('name'=>'North Carolina', 'abbrev'=>'NC'),
	array('name'=>'North Dakota', 'abbrev'=>'ND'),
	array('name'=>'Ohio', 'abbrev'=>'OH'),
	array('name'=>'Oklahoma', 'abbrev'=>'OK'),
	array('name'=>'Oregon', 'abbrev'=>'OR'),
	array('name'=>'Pennsylvania', 'abbrev'=>'PA'),
	array('name'=>'Rhode Island', 'abbrev'=>'RI'),
	array('name'=>'South Carolina', 'abbrev'=>'SC'),
	array('name'=>'South Dakota', 'abbrev'=>'SD'),
	array('name'=>'Tennessee', 'abbrev'=>'TN'),
	array('name'=>'Texas', 'abbrev'=>'TX'),
	array('name'=>'Utah', 'abbrev'=>'UT'),
	array('name'=>'Vermont', 'abbrev'=>'VT'),
	array('name'=>'Virginia', 'abbrev'=>'VA'),
	array('name'=>'Washington', 'abbrev'=>'WA'),
	array('name'=>'West Virginia', 'abbrev'=>'WV'),
	array('name'=>'Wisconsin', 'abbrev'=>'WI'),
	array('name'=>'Wyoming', 'abbrev'=>'WY')
	);

	$return = false;
	foreach ($states as $state) {
		if ($to == 'name') {
			if (strtolower($state['abbrev']) == strtolower($name)){
				$return = $state['name'];
				break;
			}
		} else if ($to == 'abbrev') {
			if (strtolower($state['name']) == strtolower($name)){
				$return = strtoupper($state['abbrev']);
				break;
			}
		}
	}
	return $return;
}

The function basically accepts a string of text ($name) and a keyword which can either be the ‘name’ or ‘abbrev’. This tells the function what you want from it (ie: if you told it Florida and then ‘abbrev’ for the argument it would return FL).

What if I dont know what data I have but know what data I want out?

This problem will come up quite frequently if, for example, you are compiling data from various sources and you are parsing mixed input. To make the function a little less stupid we need to modify the foreach loop at the bottom a little.

foreach ($states as $state) {
	foreach ($state as $title=>$value) {
		if (strtolower($value) == strtolower(trim($name))) {
			if ($to == 'name') {
				$return = $state['name'];
			} else {
				$return = $state['abbrev'];
			}
			break;
		}
	}
}

This code, instead of checking the specific field for each state, check the whole state array for the text we passed it. This way it doesn’t care if your passing it a state name or abbreviation as it can match on both. As before the second function argument is what response you want.

Whats next?

The next thing to do with this would be to just chuck the State details into a database and query them which would be much faster. The advantage about my way is that you can use it when writing code for others or systems that are unknown to you.

hope that has saved you a little time!

11 comments

  1. James says:

    The problem with the code is that there is a typo in the array. The second elementshould read “abbrev” and you have it reading “apprev”, so the function doesnt work.

    Best,

    James

    • Sean says:

      Hi James,

      you are indeed correct. For some reason it worked at the time. Must have been either a fluke or me being negligent :) Sorted now though!

      thanks
      Sean

  2. Jason says:

    Code works great and saved me a hassle. Thanks for sharing.

  3. jwerre says:

    Thanks James, this is helpful. One suggestion:

    if ($state['name'] == strtoupper($name)){
    $return = $state['abbrev'];
    break;
    }

    No need to change case to lower then uppper and then back again

    • Sean says:

      Hi Jonah, not surely sure what you mean there but on revisiting my code I am being a little bit overeager with the case changes. Abbreviations will always be upper case so it doesn’t need changing. The strtolowers on the name check are, however, relevant as remember half of the check is coming from an unknown source and needs to be sanitised.

      The line $return = strtoupper($state['abbrev']); can just be $return = $state['abbrev']; if we were being fussy but ultimately it doesn’t make a great deal of difference.

      thanks
      Sean

  4. LeberMac says:

    Sean, great PHP script, and useful!

    I do a good bit of mailing work over here in the states, and I think I’ll crib your PHP code and work something up.

    One note, you could also add
    Puerto Rico –> PR
    Guam –> GU

    And also – adding the Canadian province abbreviations would be super-useful, a lot of times when mailing we’ll get some Canadian addresses thrown in there.

    Thanks and keep rockin’ the code…

  5. Chris Kirk says:

    Great function. Thanks for saving me some time!

  6. Ed says:

    really newbie question but once I have the above function in my function file how can I call it?

  7. Pablo Carrau says:

    Added some additional features and states to the original function:

    /* Convert State Name/Abbrev/Code Function */
    function convertState($strInput, $strFormat=’name’, $blnDefaultToInput=false) {
    $arrStates = array(
    array(‘code’=>’01′, ‘abbrev’=>’AL’, ‘name’=>’Alabama’),
    array(‘code’=>’02′, ‘abbrev’=>’AK’, ‘name’=>’Alaska’),
    array(‘code’=>’04′, ‘abbrev’=>’AZ’, ‘name’=>’Arizona’),
    array(‘code’=>’05′, ‘abbrev’=>’AR’, ‘name’=>’Arkansas’),
    array(‘code’=>’06′, ‘abbrev’=>’CA’, ‘name’=>’California’),
    array(‘code’=>’08′, ‘abbrev’=>’CO’, ‘name’=>’Colorado’),
    array(‘code’=>’09′, ‘abbrev’=>’CT’, ‘name’=>’Connecticut’),
    array(‘code’=>’10′, ‘abbrev’=>’DE’, ‘name’=>’Delaware’),
    array(‘code’=>’11′, ‘abbrev’=>’DC’, ‘name’=>’District of Columbia’),
    array(‘code’=>’11′, ‘abbrev’=>’DC’, ‘name’=>’Washington DC’),
    array(‘code’=>’11′, ‘abbrev’=>’DC’, ‘name’=>’Washington D.C.’),
    array(‘code’=>’12′, ‘abbrev’=>’FL’, ‘name’=>’Florida’),
    array(‘code’=>’13′, ‘abbrev’=>’GA’, ‘name’=>’Georgia’),
    array(‘code’=>’15′, ‘abbrev’=>’HI’, ‘name’=>’Hawaii’),
    array(‘code’=>’16′, ‘abbrev’=>’ID’, ‘name’=>’Idaho’),
    array(‘code’=>’17′, ‘abbrev’=>’IL’, ‘name’=>’Illinois’),
    array(‘code’=>’18′, ‘abbrev’=>’IN’, ‘name’=>’Indiana’),
    array(‘code’=>’19′, ‘abbrev’=>’IA’, ‘name’=>’Iowa’),
    array(‘code’=>’20′, ‘abbrev’=>’KS’, ‘name’=>’Kansas’),
    array(‘code’=>’21′, ‘abbrev’=>’KY’, ‘name’=>’Kentucky’),
    array(‘code’=>’22′, ‘abbrev’=>’LA’, ‘name’=>’Louisiana’),
    array(‘code’=>’23′, ‘abbrev’=>’ME’, ‘name’=>’Maine’),
    array(‘code’=>’24′, ‘abbrev’=>’MD’, ‘name’=>’Maryland’),
    array(‘code’=>’25′, ‘abbrev’=>’MA’, ‘name’=>’Massachusetts’),
    array(‘code’=>’26′, ‘abbrev’=>’MI’, ‘name’=>’Michigan’),
    array(‘code’=>’27′, ‘abbrev’=>’MN’, ‘name’=>’Minnesota’),
    array(‘code’=>’28′, ‘abbrev’=>’MS’, ‘name’=>’Mississippi’),
    array(‘code’=>’29′, ‘abbrev’=>’MO’, ‘name’=>’Missouri’),
    array(‘code’=>’30′, ‘abbrev’=>’MT’, ‘name’=>’Montana’),
    array(‘code’=>’31′, ‘abbrev’=>’NE’, ‘name’=>’Nebraska’),
    array(‘code’=>’32′, ‘abbrev’=>’NV’, ‘name’=>’Nevada’),
    array(‘code’=>’33′, ‘abbrev’=>’NH’, ‘name’=>’New Hampshire’),
    array(‘code’=>’34′, ‘abbrev’=>’NJ’, ‘name’=>’New Jersey’),
    array(‘code’=>’35′, ‘abbrev’=>’NM’, ‘name’=>’New Mexico’),
    array(‘code’=>’36′, ‘abbrev’=>’NY’, ‘name’=>’New York’),
    array(‘code’=>’37′, ‘abbrev’=>’NC’, ‘name’=>’North Carolina’),
    array(‘code’=>’38′, ‘abbrev’=>’ND’, ‘name’=>’North Dakota’),
    array(‘code’=>’39′, ‘abbrev’=>’OH’, ‘name’=>’Ohio’),
    array(‘code’=>’40′, ‘abbrev’=>’OK’, ‘name’=>’Oklahoma’),
    array(‘code’=>’41′, ‘abbrev’=>’OR’, ‘name’=>’Oregon’),
    array(‘code’=>’42′, ‘abbrev’=>’PA’, ‘name’=>’Pennsylvania’),
    array(‘code’=>’44′, ‘abbrev’=>’RI’, ‘name’=>’Rhode Island’),
    array(‘code’=>’45′, ‘abbrev’=>’SC’, ‘name’=>’South Carolina’),
    array(‘code’=>’46′, ‘abbrev’=>’SD’, ‘name’=>’South Dakota’),
    array(‘code’=>’47′, ‘abbrev’=>’TN’, ‘name’=>’Tennessee’),
    array(‘code’=>’48′, ‘abbrev’=>’TX’, ‘name’=>’Texas’),
    array(‘code’=>’49′, ‘abbrev’=>’UT’, ‘name’=>’Utah’),
    array(‘code’=>’50′, ‘abbrev’=>’VT’, ‘name’=>’Vermont’),
    array(‘code’=>’51′, ‘abbrev’=>’VA’, ‘name’=>’Virginia’),
    array(‘code’=>’53′, ‘abbrev’=>’WA’, ‘name’=>’Washington’),
    array(‘code’=>’54′, ‘abbrev’=>’WV’, ‘name’=>’West Virginia’),
    array(‘code’=>’55′, ‘abbrev’=>’WI’, ‘name’=>’Wisconsin’),
    array(‘code’=>’56′, ‘abbrev’=>’WY’, ‘name’=>’Wyoming’),
    array(‘code’=>’60′, ‘abbrev’=>’AS’, ‘name’=>’American Samoa’),
    array(‘code’=>’66′, ‘abbrev’=>’GU’, ‘name’=>’Guam’),
    array(‘code’=>’69′, ‘abbrev’=>’MP’, ‘name’=>’Northern Mariana Islands’),
    array(‘code’=>’72′, ‘abbrev’=>’PR’, ‘name’=>’Puerto Rico’),
    array(‘code’=>’78′, ‘abbrev’=>’VI’, ‘name’=>’Virgin Islands’),
    array(‘code’=>’64′, ‘abbrev’=>’FM’, ‘name’=>’Federated States of Micronesia’),
    array(‘code’=>’68′, ‘abbrev’=>’MH’, ‘name’=>’Marshall Islands’),
    array(‘code’=>’70′, ‘abbrev’=>’PW’, ‘name’=>’Palau’)
    );

    $strOutput = ($blnDefaultToInput) ? $strInput : ”;
    $strFormat = strtolower(trim($strFormat));

    foreach ($arrStates as $arrState) {
    foreach ($arrState as $strValue) {
    if (strtolower($strValue) == strtolower(trim($strInput))) {
    if ($strFormat == ‘code’) {
    $strOutput = $arrState['code'];
    }
    else if ($strFormat == ‘abbrev’) {
    $strOutput = $arrState['abbrev'];
    }
    else {
    $strOutput = $arrState['name'];
    }
    break;
    }
    }
    }

    return $strOutput;
    }

Leave a Reply