Jump to content

Advanced Comparisons


Taorluath

Recommended Posts

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?

 

 

Link to comment
https://forums.phpfreaks.com/topic/138080-advanced-comparisons/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/138080-advanced-comparisons/#findComment-721798
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.