Try using a switch statement.
Try something like this:
PHP Code:
<form method="post">
Message: <input name="message" type="text" />
<label><input type="radio" name="type" value="switch" checked="checked" />Switch case</label>
<label><input type="radio" name="type" value="preg_replace" />Preg_replace</label>
<br />
<input type="submit" value="Send Message" />
<?php
$message = $_POST['message'];
$type = $_POST['type'];
if ($type == 'switch')
{
switch(strtolower($message))
{
case 'hello':
$return = 'How are you?';
break;
case 'good you?':
$return = 'I am good.';
break;
case 'good':
$return = 'That\'s cool.';
break;
case 'sad':
$return = 'Well, why are you sad?';
break;
case 'sad you?':
$return = 'I\'m good, and why so sad?';
break;
case 'fine':
$return = "Fine, usually that means you're bored!";
break;
case 'fine you?':
$return = 'I\'m good.';
break;
default:
$return = 'Why hello!';
break;
}
}
else if ($type == 'preg_replace')
{
$find = array(
"/(.*?)hello(.*?)/is",
"/(.*?)good(.*?)you\?(.*?)/is",
"/(.*?)good(.*?)/is",
"/(.*?)sad(.*?)/is",
"/(.*?)sad(.*?)you\?(.*?)/is",
"/(.*?)fine(.*?)/is",
"/(.*?)fine(.*?)you\?(.*?)/is"
);
$replace = array(
"How are you?",
"I'm good.",
"That's cool!",
"Well, why are you sad?",
"I'm good, and why so sad?",
"Fine, usually that means you're bored!",
"I'm good."
);
$return = preg_replace($find, $replace, $message);
}
echo $return;
?>
<br />