Jump to content

[SOLVED] Need help with lookup and replace


cyberkiller

Recommended Posts

I have a variable coming in with a state abbreviation which I want to change to the full name of the state. I am pretty new to php and have no clue how I should code it or where to start. I need to do it for all 50 states. I included some pseudo code below.

 

$variable1

$variable2

 

if variable = la

variable2 = los angeles

if variable = ca

variable2 = california

 

etc.. for all 50 states.

Edit: same concept as above with some letter case handling and error checking

 

You would use an array -

 

<?php
$lookup = array(); // use lowercase index names so that you can deal with any combination entered by converting it to lowercase
$lookup['ca'] = "California";
$lookup['co'] = "Colorado";
// add rest of entries

// $state contains the abbreviation you want to lookup
$state = 'CA';

$state = strtolower($state); // convert to lower case in case it was entered uc or a mix of uc/lc
if(isset($lookup[$state])){
    echo $lookup[$state];
} else {
    echo "Not found<br />";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.