Page 1 of 1

Same word different "grammar"

Posted: Sun Jul 31, 2011 4:15 pm
by EcazS
I have something like this,
if($_GET['category'] === "all") {
     //stuff
}
This only works if it's exactly "all" what if I want to run the code even if it's "ALL" or "aLl" or "aLL".

I'm hoping there is a better way than doing a bunch of || in the if statement :lol:

Re: Same word different "grammar"

Posted: Sun Jul 31, 2011 7:11 pm
by Kamal
if(strtolower($_GET['category']) === "all") {
     //stuff
}
?

Re: Same word different "grammar"

Posted: Sun Jul 31, 2011 8:28 pm
by EcazS
Using strtolower works to 50% percent. I was looking for something different.

I'll post again when I know how to explain it better :S

Re: Same word different "grammar"

Posted: Sun Jul 31, 2011 8:36 pm
by jacek
Do you want words that are a synonym of "all" to also work ?

Re: Same word different "grammar"

Posted: Mon Aug 01, 2011 6:24 am
by Kamal
Maybe create an array containing allowed forms of "all", then use the in_array function.

Re: Same word different "grammar"

Posted: Wed Aug 03, 2011 8:28 am
by Tino
You could use strcasecmp to check if it's the same spelling but case-insensitive.
$word1 = 'test';
$word2 = 'Test';

if ( strcasecmp($word1, $word2) === 0 ) {
    echo 'They are the same!';
}
Maybe create an array containing allowed forms of "all", then use the in_array function.
Now what if you had a different word? For a three-letter word, there are only eight different combinations. However, even if you have only an eight-letter word, you already have 256 combinations! Ten letters? 1024 combinations!

Seems like that would get way too tedious ;)

Re: Same word different "grammar"

Posted: Wed Aug 03, 2011 2:57 pm
by EcazS
Tino wrote:Now what if you had a different word? For a three-letter word, there are only eight different combinations. However, even if you have only an eight-letter word, you already have 256 combinations! Ten letters? 1024 combinations!

Seems like that would get way too tedious ;)
Unless you use strtolower before checking it, or that other thing you posted.

Re: Same word different "grammar"

Posted: Wed Aug 03, 2011 3:18 pm
by Tino
Indeed.

What kamal suggested was something like this though, I think:
$word = 'aLL';

$combinations = array('all', 'All', 'aLl', 'alL', 'ALl', 'AlL', 'aLL', 'ALL');

if ( in_array(word, $combinations) ) { echo "It's good!"; }
Which in itself is a fine solution, but, as I said, way too tedious for longer words. I mean, I don't really want to create a 1024 item long array with all different ways to write 'homeless' or something.