View Full Version : Need a very simple word filter
ntin0s
11-17-2010, 06:07 PM
Hello I am a student and new to web developing so i need your help,
I need to put a word filter to my chatbot so it will echo only what the user wrote after the echo. So if the user types "echo Hello" the bot will echo "Hello".
Here is my code
<?php
echo $_REQUEST['msg'];
?>
Thanks in advance
Schmoopy
11-17-2010, 06:25 PM
Without a great knowledge of regexp I'd do it this way:
// Input string ($_REQUEST['msg'] in your case)
$str = 'echo Hello';
// 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);
// Echoes "Hello"
Probably better using regexp, this will work as long as your just using "echo". If you need it to do different things depending on what the user types, like special keywords, then please mention it.
ntin0s
11-17-2010, 06:34 PM
It's not working. I just need the word echo not to be displayed if is written
Schmoopy
11-17-2010, 06:40 PM
I don't understand, that's what the above code does, as long as "echo" is at the beginning of the string, it will echo everything after it.
Please try to clarify exactly what you want it to do, with a few examples if possible.
ntin0s
11-17-2010, 06:49 PM
Ok i found the problem it was my mistake because im confused with so many files. Anyway the problem now is that if i type " echo something" it always echoes "Hello". So your code works but it always answers Hello. I need it to answer back what i wrote after echo
Schmoopy
11-17-2010, 06:54 PM
Oh right, I was just using $str as an example, this should work for you:
$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);
ntin0s
11-17-2010, 07:00 PM
Thank you very much and sorry for not understanding. Now when i type "echo something" it answers back "something" as i wanted to but there is another problem now. If i type just "hi" without the echo at the beginning it gives no answer, just blank.
Schmoopy
11-17-2010, 07:01 PM
What do you want it to say if "echo" is not at the beginning?
ntin0s
11-17-2010, 07:04 PM
if "echo" is not at the beginning i want it to say what i wrote. For example :
User says: hello bot
Bot says: hello bot
if
User says:echo hello bot
Bot says: hello bot
The problem now is that if
User says: hello bot
Bot says:
Schmoopy
11-17-2010, 07:06 PM
<?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;
?>
ntin0s
11-17-2010, 07:11 PM
http://uploadpic.org/storage/originals/thumb_ks322fj8d2rsufnd2uf9e3f2ln.jpg (http://uploadpic.org/view-pic.php?img=114314)
Thanks a lot
ntin0s
11-18-2010, 11:06 AM
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
Schmoopy
11-18-2010, 05:52 PM
<?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;
}
?>
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.
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().
ntin0s
11-18-2010, 11:12 PM
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.
ntin0s
11-18-2010, 11:37 PM
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.
james438
11-19-2010, 02:53 AM
I'm just reading, but I wanted to suggest that you post the code that you are using for your edit/delete page(s).
Schmoopy
11-19-2010, 07:05 AM
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:
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-19-2010, 07:31 PM
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.";
}
?>
Schmoopy
11-20-2010, 12:07 PM
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
$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, 10:48 PM
Thats perfect!
ntin0s
11-28-2010, 09:30 PM
Hello again,
If i want to connect it with gtalk and msn what i should do?
djr33
11-28-2010, 11:39 PM
You'll have to look to see if they have an API. Be aware it's very complex.
MSN: http://msdn.microsoft.com/en-us/windowslive/ff759527.aspx
GTalk: http://code.google.com/apis/talk/
Be aware it's very complex.
ntin0s
11-29-2010, 10:04 AM
Done. Now I need if the user type quote it will randomly echo a quote from a text file that i have with quotes numbered Quote1 , Quote2 etc. It can be done?
<?php
function quote($file){
$split = explode("\n", $file);
return $split[rand(0, count($split)-1)];
}
echo quote(file_get_contents("quotes.txt"));
?>
quotes.txt:
Quote 1
Quote 2
Quote 3
Quote 4
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.