views:

153

answers:

5

So I have some code here that takes user input from a standard web form:

if (get_magic_quotes_gpc()) {
$searchsport = stripslashes($_POST['sport']);
$sportarray = array(
"Football" => "Fb01",
"Cricket" => "ck32",
"Tennis" => "Tn43",
);
if(isset($sportarray[$searchsport])){
header("Location: ".$sportarray[$searchsport].".html");
die;
}

How would I go about modifying this (I think the word is parsing?) to make it case *in*sensitive? For example, I type in "fOoTbAlL" and php will direct me to Fb01.html normally.

EDIT: Note that the code is just an example, the string entered by the user can contain more than one word say "Crazy aWesOme HarpOOn-Fishing" and it would still work if the array element "Crazy Awesome Harpoon-Fishing" (take note of the capital F before the dash).

+2  A: 

I would use a string function strtolower(). http://uk3.php.net/strtolower

DavidYell
+1  A: 
$searchsport = strtolower($_POST['sport']);
$sportarray = array(
"football" => "Fb01",
"cricket" => "ck32",
"tennis" => "Tn43",
);
if(isset($sportarray[$searchsport])){
header("Location: ".$sportarray[$searchsport].".html");
die;
}

In this way the search string and the array keys are both lowercase and you can do a case insensitive comparison.

If you want to preserve the case of the $sportarray keys you can do:

$searchsport = ucfirst(strtolower($_POST['sport']));
$sportarray = array(
"Football" => "Fb01",
"Cricket" => "ck32",
"Tennis" => "Tn43",
);
if(isset($sportarray[$searchsport])){
header("Location: ".$sportarray[$searchsport].".html");
die;
}
mck89
Hi, thanks for the answer. Just wondering if I used stripslashes with this properly:$searchitem = ucfirst(strtolower(stripslashes($_POST["item"])));? I've tried your code everything works fine except for the stripslashes.
Haskella
What's the error with stripslashes?
mck89
@mck89, I'll edit my first post.
Haskella
Ahh, I think I understand now why it isn't working. There is nothing wrong with your code. The problem is the ucfirst function which only converts the first word. If I had Lawn Bowls in my array it will not match. Instead when I type lawn bowls, it would be Lawn bowls. Am I correct?
Haskella
Then you can use ucwords instead of ucfirst
mck89
Does ucwords recognise "a-word-like-this" as 4 words or just 1?
Haskella
I don't know test it but you can replace every - with a space if you want
mck89
+2  A: 

easiest way is to use strtolower to make everything lowercase to make your comparison

zaphod
A: 
Mihai Iorga
This will never match as the items in `$sportarray` aren't lowercase!
Josh
This will never match, unless you make your array keys lower case too. </nitpick>
ZombieSheep
ups ... i didn;'t notice ... I'll reedit.
Mihai Iorga
Same comment I made for mck89, this will not work if the string entered contains 2 or more words.
Haskella
+2  A: 

You can modify your code like this:

// searches for values in case-insensitive manner
function in_arrayi($needle, $haystack) {
    return in_array(strtolower($needle), array_map('strtolower', $haystack));
}

$searchsport = $_POST['sport'];
$sportarray = array(
"Football" => "Fb01",
"Cricket" => "ck32",
"Tennis" => "Tn43",
);
if(in_arrayi($searchsport, $sportarray)){
header("Location: ".$sportarray[$searchsport].".html");
die;
}
Sarfraz
+1 : This is to the point answer.
Web Logic
The problem I have with this approach is that you're calling strtolower four times rather than just one.
King Isaac
@King: I think it is not, please see: http://www.php.net/manual/en/function.in-array.php#89256
Web Logic
You are not converting to lower case the variable in the string that you pass into the header function so it won't match
mck89
@mck89: Did you even try it?
Web Logic
Yes and it doesn't work but maybe i'm using a different version of php
mck89
@mck89: Yup, version? let the OP try, works for me.
Web Logic
5.2.something...It doesn't return true to the condition i don't know why
mck89
Not working for me either... nothing is returned.
Haskella
@Haskella: Is there any error you get?
Sarfraz
No warning message, no output at all. Can you incorporate the stripslashes like we did before also?
Haskella
@Haskella: Try with others' answers with `strtolower`.
Sarfraz