Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

State name to 2 letter code (See related posts)

Simple one-trick-pony PHP function to take a state name (case insensitive) and return the 2 letter abbreviation.

        function state_to_twoletter( $state_name ) {
                
                $state = array();
                $state['ALABAMA']='AL';
                $state['ALASKA']='AK';
                $state['AMERICAN SAMOA']='AS';
                $state['ARIZONA']='AZ';
                $state['ARKANSAS']='AR';
                $state['CALIFORNIA']='CA';
                $state['COLORADO']='CO';
                $state['CONNECTICUT']='CT';
                $state['DELAWARE']='DE';
                $state['DISTRICT OF COLUMBIA']='DC';
                $state['FEDERATED STATES OF MICRONESIA']='FM';
                $state['FLORIDA']='FL';
                $state['GEORGIA']='GA';
                $state['GUAM']='GU';
                $state['HAWAII']='HI';
                $state['IDAHO']='ID';
                $state['ILLINOIS']='IL';
                $state['INDIANA']='IN';
                $state['IOWA']='IA';
                $state['KANSAS']='KS';
                $state['KENTUCKY']='KY';
                $state['LOUISIANA']='LA';
                $state['MAINE']='ME';
                $state['MARSHALL ISLANDS']='MH';
                $state['MARYLAND']='MD';
                $state['MASSACHUSETTS']='MA';
                $state['MICHIGAN']='MI';
                $state['MINNESOTA']='MN';
                $state['MISSISSIPPI']='MS';
                $state['MISSOURI']='MO';
                $state['MONTANA']='MT';
                $state['NEBRASKA']='NE';
                $state['NEVADA']='NV';
                $state['NEW HAMPSHIRE']='NH';
                $state['NEW JERSEY']='NJ';
                $state['NEW MEXICO']='NM';
                $state['NEW YORK']='NY';
                $state['NORTH CAROLINA']='NC';
                $state['NORTH DAKOTA']='ND';
                $state['NORTHERN MARIANA ISLANDS']='MP';
                $state['OHIO']='OH';
                $state['OKLAHOMA']='OK';
                $state['OREGON']='OR';
                $state['PALAU']='PW';
                $state['PENNSYLVANIA']='PA';
                $state['PUERTO RICO']='PR';
                $state['RHODE ISLAND']='RI';
                $state['SOUTH CAROLINA']='SC';
                $state['SOUTH DAKOTA']='SD';
                $state['TENNESSEE']='TN';
                $state['TEXAS']='TX';
                $state['UTAH']='UT';
                $state['VERMONT']='VT';
                $state['VIRGIN ISLANDS']='VI';
                $state['VIRGINIA']='VA';
                $state['WASHINGTON']='WA';
                $state['WEST VIRGINIA']='WV';
                $state['WISCONSIN']='WI';
                $state['WYOMING']='WY';
                
                
                return $state[strtoupper( $state_name )]; 
                
        }


You need to create an account or log in to post comments to this site.


Related Posts