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.

Link to comment
Share on other sites

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 />";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.