Hello again,
This time i want your help making it some more complicated. What if i want to make it do things like :
repeat back a “rot13” version of what the user has said, so it should post what user types in www.rot13.com and then get the result
Also to perform a calculation, if the user msg starts with the word calc: Like this (user messages in bold, bot response in normal font):
○ calc: 3 * 4
○ 12
Thanks in advance
Last edited by ntin0s; 11-18-2010 at 11:45 AM.
The problem lies with the "+" when using "calc" if you are using $_GET, because it's treated differently when it's used in the URL and so gets ignored.PHP Code:<?php
$str = (isset($_REQUEST['msg'])) ? $_REQUEST['msg'] : '';
// Get the first space
$space = strpos($str, ' ');
$cmd = strtolower(substr($str, 0, $space));
$val = substr($str, $space);
switch($cmd) {
case 'echo':
echo $val;
break;
case 'rot13':
echo str_rot13($val);
break;
case 'calc':
$calc = trim($val);
$split = explode(' ', $calc);
$val = doCalc($split[0], $split[1], $split[2]);
echo $val;
break;
default:
echo $str;
}
function doCalc($a, $op, $b) {
$val = '';
switch($op) {
case '*':
$val = ($a * $b);
break;
case '+':
$val = ($a + $b);
break;
case '-':
$val = ($a - $b);
break;
case '/':
$val = ($a / $b);
break;
}
return $val;
}
?>
I wouldn't recommend doing calculations this way.
This also won't work if you need to do a more complex calculation (3 * 4 + 5). It will only work for 2 numbers and 1 operator at the moment.
I haven't put any error checking in for this either, so if you do something like "calc hello", it will screw it up. You shouldn't find it too difficult to get around this problem however.
Use isset().
Last edited by Schmoopy; 11-18-2010 at 07:24 PM.
ntin0s (11-20-2010)
Yeah I will sort this out. Thank you very much Schmoopy you are very very helpfull.
I have this code at the moment
<?php
$str = $_REQUEST['msg'];
// Get the first 4 characters of the string
$sub = substr($str, 0, 4);
// If it matches then echo out the latter part of the string
if($sub == 'echo')
echo substr($str, 4);
else
echo $str;
?>
but i want it to repeat back what the user has said, if their IM starts with the word "echo:" Like this:
Bot: echo: Hello world!
User: Hello world!
if the IM does not start with word echo: then the bot should reply like this:
Bot: Hello world!
User: I’ m sorry, I don’t understand.
Also I am having problems with another website im working on. I can't manage to make my Edit and Delete button for editing and deleting a user from my database.
I'm just reading, but I wanted to suggest that you post the code that you are using for your edit/delete page(s).
To choose the lesser of two evils is still to choose evil. My personal site
I'll just stick to this topic for now, if you need help with the other problems with editing and deleting, it's probably best to create a new thread.
To sort the problem with the bot returning an error if the command was incorrect, change the following part of the code to:
PHP Code:switch($cmd) {
case 'echo':
echo $val;
break;
case 'rot13':
echo str_rot13($val);
break;
case 'calc':
$calc = trim($val);
$split = explode(' ', $calc);
$val = doCalc($split[0], $split[1], $split[2]);
echo $val;
break;
default:
// The command wasn't found - display error
echo "I'm sorry, I don't understand.";
}
ntin0s (11-20-2010)
everything works except calc, i get this error :
: Call to undefined function doCalc() in on line
here is my code:
<?php
$str = (isset($_REQUEST['msg'])) ? $_REQUEST['msg'] : '';
// Get the first space
$space = strpos($str, ' ');
$cmd = strtolower(substr($str, 0, $space));
$val = substr($str, $space);
switch($cmd) {
case 'echo':
echo $val;
break;
case 'rot13':
echo str_rot13($val);
break;
case 'calc':
$calc = trim($val);
$split = explode(' ', $calc);
$val = doCalc($split[0], $split[1], $split[2]);
echo $val;
break;
default:
// The command wasn't found - display error
echo "I'm sorry, I don't understand.";
}
?>
When I posted the above code, I meant just change that specific part of it.
If you remove the doCalc() function you will get that error. To avoid confusion, here is the full working version:
PHP Code:<?php
$str = (isset($_REQUEST['msg'])) ? $_REQUEST['msg'] : '';
// Get the first space
$space = strpos($str, ' ');
$cmd = strtolower(substr($str, 0, $space));
$val = substr($str, $space);
switch($cmd) {
case 'echo':
echo $val;
break;
case 'rot13':
echo str_rot13($val);
break;
case 'calc':
$calc = trim($val);
$split = explode(' ', $calc);
$val = doCalc($split[0], $split[1], $split[2]);
echo $val;
break;
default:
// The command wasn't found - display error
echo "I'm sorry, I don't understand.";
}
function doCalc($a, $op, $b) {
$val = '';
switch($op) {
case '*':
$val = ($a * $b);
break;
case '+':
$val = ($a + $b);
break;
case '-':
$val = ($a - $b);
break;
case '/':
$val = ($a / $b);
break;
}
return $val;
}
?>
ntin0s (11-20-2010)
Thats perfect!
Bookmarks