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