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

May 24, 2008 | Time Savers | 15 comments

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'];
		} else if ($to == 'abbrev') {
			if (strtolower($state['name']) == strtolower($name)){
				$return = strtoupper($state['abbrev']);
	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'];

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!

A Donate Button!


  1. James

    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.



    • Sean

      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!


  2. Jason

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

  3. jwerre

    Thanks James, this is helpful. One suggestion:

    if ($state[‘name’] == strtoupper($name)){
    $return = $state[‘abbrev’];

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

    • Sean

      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.


  4. LeberMac

    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

    Great function. Thanks for saving me some time!

  6. Ed

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

    • Sean

      Hi Ed,

      Just use something like this:

      echo convert_state(‘Nevada’);

      Which will return and subsequently echo the appropriate code.


  7. Pablo Carrau

    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’];

    return $strOutput;

  8. Jay

    Huge time-saver – thank you!

  9. vadim

    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 and Labrador’, ‘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’, ‘abbrev’=>’YT’)

    • Sean Barton

      Thanks Vadim,

      For those people who aren’t aware of what these are, they are the Canadian areas (territories, counties, ?) to add to the array to make it work for both the USA and Canada.


  10. Jason

    I just wanted to thank you for your kindness in sharing this. It saved me a ton of time!!!



  1. Javascript to convert between states and abbreviations | Redact Web - [...] from: In this post, Sean Barton provides a nifty script for converting between U.S. states and their [...]

Submit a Comment

Your email address will not be published. Required fields are marked *

CommentLuv badge

Stay in touch!

Page Builder Cloud

Page Builder Cloud

A truly universal template library for WordPress Page Builders.

Page Builder Recommendation

Elementor Banner

We are BIG fans of the Elementor page builder. Give it a look!

About this site and Sean Barton

Picture of Sean
Sean Barton is a Freelance PHP Website Developer in Crewe, Cheshire. He is a Wordpress and CMS/Framework specialist and Co-Founder of Page Builder Cloud.
This site was set up in 2008 as a tutorial and scripting resource for the PHP language and Wordpress.
Find out more about Sean on the About Me page or use the Hire Me page to get in touch. For more information about Sean's work take a look at the Portfolio

Our Services

  • Wordpress plugin/theme development
  • Divi specialist
  • Ecommerce (Woocommerce, WPSC, Shopify, Magento)
  • PSD to Wordpress theme conversion (Responsive)
  • Website design work (Banners, Logos, Full Site, etc)
  • Website analysis (security, usability, SEO)
  • API Integrations (InfusionSoft, SalesForce, Ontraport, Customer Thermometer, etc..)
  • Wordpress consultancy & expert advice
  • Crisis support
  • Website hosting

The main services offered are Wordpress based although we do a great deal of technical programming for bespoke systems. From troubleshooting, extending frameworks, finding bugs to writing them from scratch.

Find out more by looking through our past projects or get a quote.

Be the first to hear about new products/updates!

This is a mailing list for those people interested in being told when we release a new product (Divi plugin or Theme).

We shall also use this list to let you know about product updates and releases.

You have Successfully Subscribed!