Taorluath Posted December 22, 2008 Share Posted December 22, 2008 So here's the deal: I'm designing this kind of 'chat bot' in php. I want it to evaluate various ways of saying the same thing, such as "no" and "No", before it responds. I could have an 'if' statement that covers every possible input by itself, but that seems very unwieldy. if ($test==("no" or "no." or "no!" or "No" or "No." etc...)) I've been trying to make a multidimensional 'or' expression, but it seems like any second-level 'or' statement is just considered always true. Like with this: $a = ("." or "!"); //$a is always true if ($test==("no".$a)) { //also always true Below is what I'm kind of trying to get. $test should have to be either "no.", "no!", or "NO" to get the first response. Instead, it always outputs "you entered no". if ($test==("no".("!" or ".") or "NO")) { echo "you entered no"; } else { echo "yes?"; } I like how with regex, it could be something like "no(.|!)", can you do this with php? Quote Link to comment Share on other sites More sharing options...
DarkerAngel Posted December 22, 2008 Share Posted December 22, 2008 if(preg_match("#[^a-zA-Z0-9]no[^a-zA-Z0-9]#i", $test)) { ... } just something to start with Quote Link to comment Share on other sites More sharing options...
kratsg Posted December 22, 2008 Share Posted December 22, 2008 For case-insensitive things, I find that the best way to handle this is to convert the messages into lowercase by default: strtolower(); [i believe this is the function] and work with that string. This way, you don't have to worry about crap such as mixed cases and such. IE: <?php $message = "HeLlO WORLd"; $string = strtolower($message); if($string == "hello world"){ if($message == "hello world"){ echo "all lowercase"; } else { echo "mixed cases"; } else { echo "No command set for this string"; } ?> This is pretty straightforward and a plus is being able to output the original string if you wanted the bot to quote someone while you're still able to compare parts of it in lowercase form :-) You should get the idea. Quote Link to comment Share on other sites More sharing options...
DarkerAngel Posted December 22, 2008 Share Posted December 22, 2008 my preg_match did a case insensitive search also I guess this works too: if(preg_match("/\Wno\W/i", $test); Quote Link to comment Share on other sites More sharing options...
Taorluath Posted December 23, 2008 Author Share Posted December 23, 2008 Woohoo! Thanks you guys, this forum rocks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.