/* Convert full state to abbreviation */
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'),
array('name'=>'Alberta', 'abbrev'=>'AB'),
array('name'=>'British Columbia', 'abbrev'=>'BC'),
array('name'=>'Manitoba', 'abbrev'=>'MB'),
array('name'=>'New Brunswick', 'abbrev'=>'NB'),
array('name'=>'Newfoundland', 'abbrev'=>'NL'),
array('name'=>'Northwest Territories', 'abbrev'=>'NT'),
array('name'=>'Nova Scotia', 'abbrev'=>'NS'),
array('name'=>'Nunavut', 'abbrev'=>'NU'),
array('name'=>'Ontario', 'abbrev'=>'ON'),
array('name'=>'Prince Edward Island', 'abbrev'=>'PE'),
array('name'=>'Quebec', 'abbrev'=>'QC'),
array('name'=>'Saskatchewan', 'abbrev'=>'SK'),
array('name'=>'Yukon Territory', 'abbrev'=>'YT')
);
$return = false;
foreach ($states as $state) {
foreach ($state as $title=>$value) {
if (strtolower($value) == strtolower(trim($name))) {
if ($to == 'name') {
$return = $state['abbrev'];
} else {
$return = $state['name'];
}
break;
}
}
}
return $return;
}
You can also reverse this function and switch from abbreviation to full name by updating the if statement at the bottom.
if (strtolower($value) == strtolower(trim($name))) {
if ($to == 'name') {
$return = $state['name'];
} else {
$return = $state['abbrev'];
}
break;
}
if (strtolower($value) == strtolower(trim($name))) {
if ($to == 'name') {
$return = $state['name'];
} else {
$return = $state['abbrev'];
}
break;
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.