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
methos
Forum Newbie
Posts: 13 Joined: Sat Oct 21, 2006 8:31 am
Post
by methos » Tue Feb 20, 2007 6:18 pm
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
<?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.
WaldoMonster
Forum Contributor
Posts: 225 Joined: Mon Apr 19, 2004 6:19 pm
Contact:
Post
by WaldoMonster » Tue Feb 20, 2007 6:36 pm
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;
?>
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Feb 20, 2007 6:41 pm
I think switch is better for this, but the concept transfers nicely.
WaldoMonster
Forum Contributor
Posts: 225 Joined: Mon Apr 19, 2004 6:19 pm
Contact:
Post
by WaldoMonster » Tue Feb 20, 2007 7:04 pm
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 » Tue Feb 20, 2007 8:57 pm
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;
?>
onion2k
Jedi Mod
Posts: 5263 Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com
Post
by onion2k » Wed Feb 21, 2007 2:23 am
You don't store your countries in a database? Odd.