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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.