Make Switch-Case function case insensitive

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
methos
Forum Newbie
Posts: 13
Joined: Sat Oct 21, 2006 8:31 am

Make Switch-Case function case insensitive

Post by methos »

1. How could I make the following code case insensitive. For example: If the switch variable $Country would be "Uk", the code would still result in "UK" ?

2. If the switch variable $Country would be "U.K.", how could I make it result in "UK" ? How could I include dots in the case ? Such as:

Code: Select all

case U.K. : $Country="GB"; break;

Code: Select all

<?php
    switch ($Country){
// U.S.A.
        case USA : $Country="US"; break;
        case US : $Country="US"; break;
        case America : $Country="US"; break;
// U.K.
        case UK : $Country="GB"; break;
        case Kingdom : $Country="GB"; break;
        case Britain : $Country="GB"; break;

   default:
        $Country = "US";
}
Last edited by methos on Sun Feb 25, 2007 3:14 pm, edited 1 time in total.
User avatar
WaldoMonster
Forum Contributor
Posts: 225
Joined: Mon Apr 19, 2004 6:19 pm
Contact:

Post by WaldoMonster »

Here is another approach:

Code: Select all

<?php
$country = 'u.k.';

$country = strtolower($country);
if      (in_array($country, array('usa', 'us', 'america')))             $country = 'US';
elseif  (in_array($country, array('uk', 'u.k.', 'kingdom', 'britain'))) $country = 'GB';
else                                                                    $country = 'US';

echo $country;
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I think switch is better for this, but the concept transfers nicely. :)
User avatar
WaldoMonster
Forum Contributor
Posts: 225
Joined: Mon Apr 19, 2004 6:19 pm
Contact:

Post by WaldoMonster »

The string after case must be around single or double quotes.
Also here I have used strtolower().

Code: Select all

<?php
$Country = 'U.k.';

switch (strtolower($Country))
    { 
    // U.S.A. 
    case 'usa'      : $Country = 'US'; break;
    case 'us'       : $Country = 'US'; break;
    case 'america'  : $Country = 'US'; break;
    // U.K. 
    case 'u.k.'     : $Country = 'GB'; break;
    case 'uk'       : $Country = 'GB'; break;
    case 'kingdom'  : $Country = 'GB'; break;
    case 'britain'  : $Country = 'GB'; break;
    // Default
    default        : $Country = 'US';
    }

echo $Country;
?>
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

WaldoMonster wrote:The string after case must be around single or double quotes.
Also here I have used strtolower().

Code: Select all

<?php
$Country = 'U.k.';

switch (strtolower($Country))
    { 
    // U.S.A. 
    case 'usa'      : $Country = 'US'; break;
    case 'us'       : $Country = 'US'; break;
    case 'america'  : $Country = 'US'; break;
    // U.K. 
    case 'u.k.'     : $Country = 'GB'; break;
    case 'uk'       : $Country = 'GB'; break;
    case 'kingdom'  : $Country = 'GB'; break;
    case 'britain'  : $Country = 'GB'; break;
    // Default
    default        : $Country = 'US';
    }

echo $Country;
?>
You could take it a step futher...

Code: Select all

<?php
$Country = 'U.k.';

switch (strtolower($Country))
    { 
    // U.K. 
    case 'u.k.' : case 'uk' : case 'kingdom' : case 'britain' :
        $Country = 'GB';
    break;
    // U.S.A. / Default
    case 'usa' : case 'us' : case 'america' : default :
        $Country = 'US'; 
    }

echo $Country;
?>
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

You don't store your countries in a database? Odd.
Post Reply