Convert US State names to abbreviations or US abbreviations to US State names. It works both ways.

With this function you can obtain the full US State name by providing the US State abbreviation. It is also a reversible function that will automatically detect and do it in the opposite manner. That is, if you provide a full US State name it will return the corresponding US State abbreviation.

By default it will return a string using proper capitalization for the full US State names, and all capital letters for the US State abbreviations. You can also set the format parameter to return the string in all lowercase or all capital letters.

If no match is found due to an invalid lookup, then it will return an empty string.

/**
 * Get State Name
 *
 * This function allows you to retrieve a U.S. State name via its abbreviation or full name. It will automatically
 * return the counterpart. That is, if you provide a U.S. State abbreviation such as WA, this function will return
 * Washington. If instead you provide the full U.S. State name such as California, then the U.S. State abbreviation of
 * CA will be returned. You can toggle the format argument to return in all lowercase or all uppercase.
 *
 * @param string $lookup Provide either the 2-letter US ISO abbreviation or full State name to lookup.
 * @param string $format By default returns pre-formatted. Use 'lowercase' or 'uppercase' for an alternate format.
 *
 * @return string
 */
function getStateName($lookup, $format = null): string
{
    $states = [
        'AA' => 'Armed Forces Americas',
        'AE' => 'Armed Forces Europe / Canada / Middle East / Africa',
        'AK' => 'Alaska',
        'AL' => 'Alabama',
        'AP' => 'Armed Forces Pacific',
        'AR' => 'Arkansas',
        'AS' => 'American Samoa',
        'AZ' => 'Arizona',
        'CA' => 'California',
        'CO' => 'Colorado',
        'CT' => 'Connecticut',
        'DC' => 'District of Columbia',
        'DE' => 'Delaware',
        'FL' => 'Florida',
        'FM' => 'Federated States of Micronesia',
        'GA' => 'Georgia',
        'GU' => 'Guam GU',
        'HI' => 'Hawaii',
        'IA' => 'Iowa',
        'ID' => 'Idaho',
        'IL' => 'Illinois',
        'IN' => 'Indiana',
        'KS' => 'Kansas',
        'KY' => 'Kentucky',
        'LA' => 'Louisiana',
        'MA' => 'Massachusetts',
        'MD' => 'Maryland',
        'Me' => 'Maine',
        'MH' => 'Marshall Islands',
        'MI' => 'Michigan',
        'MN' => 'Minnesota',
        'MO' => 'Missouri',
        'MP' => 'Northern Mariana Islands',
        'MS' => 'Mississippi',
        'MT' => 'Montana',
        'NC' => 'North Carolina',
        'ND' => 'North Dakota',
        'NE' => 'Nebraska',
        'NH' => 'New Hampshire',
        'NJ' => 'New Jersey',
        'NM' => 'New Mexico',
        'NV' => 'Nevada',
        'NY' => 'New York',
        'OH' => 'Ohio',
        'OK' => 'Oklahoma',
        'OR' => 'Oregon',
        'PA' => 'Pennsylvania',
        'PR' => 'Puerto Rico',
        'PW' => 'Palau',
        'RI' => 'Rhode Island',
        'SC' => 'South Carolina',
        'SD' => 'South Dakota',
        'TN' => 'Tennessee',
        'TX' => 'Texas',
        'UT' => 'Utah',
        'VA' => 'Virginia',
        'VI' => 'Virgin Islands',
        'VT' => 'Vermont',
        'WA' => 'Washington',
        'WI' => 'Wisconsin',
        'WV' => 'West Virginia',
        'WY' => 'Wyoming',
    ];

    $lookup = strtoupper($lookup);

    if(! isset($states[$lookup])) {
        $states = array_change_key_case(array_flip($states), CASE_UPPER);

        if(! isset($states[$lookup])) {
            return '';
        }
    }

    if($format == 'lowercase') {
        return strtolower($states[$lookup]);
    } elseif($format == 'uppercase') {
        return strtoupper($states[$lookup]);
    }

    return $states[$lookup];
}

This code snippet was published on It was last edited on

0

0 Comments

  • Votes
  • Oldest
  • Latest