Same word different "grammar"

Ask about a PHP problem here.
Post Reply
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Same word different "grammar"

Post 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:
User avatar
Kamal
Posts: 123
Joined: Fri May 06, 2011 10:45 am
Contact:

Re: Same word different "grammar"

Post by Kamal »

if(strtolower($_GET['category']) === "all") {
     //stuff
}
?
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Same word different "grammar"

Post 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
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Same word different "grammar"

Post by jacek »

Do you want words that are a synonym of "all" to also work ?
Image
User avatar
Kamal
Posts: 123
Joined: Fri May 06, 2011 10:45 am
Contact:

Re: Same word different "grammar"

Post by Kamal »

Maybe create an array containing allowed forms of "all", then use the in_array function.
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: Same word different "grammar"

Post 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 ;)
Please check out my CodeCanyon items.
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Same word different "grammar"

Post 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.
Last edited by jacek on Wed Aug 03, 2011 6:10 pm, edited 1 time in total.
Reason: fixed quote.
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: Same word different "grammar"

Post 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.
Please check out my CodeCanyon items.
Post Reply