Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 25

Thread: Need a very simple word filter

  1. #11
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default


    Thanks a lot

  2. #12
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    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.

  3. #13
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    PHP Code:
    <?php

    $str 
    = (isset($_REQUEST['msg'])) ? $_REQUEST['msg'] : '';

    // Get the first space
    $space strpos($str' ');

    $cmd strtolower(substr($str0$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().
    Last edited by Schmoopy; 11-18-2010 at 07:24 PM.

  4. The Following User Says Thank You to Schmoopy For This Useful Post:

    ntin0s (11-20-2010)

  5. #14
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    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.

  6. #15
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    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.

  7. #16
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    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

  8. #17
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    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.";



  9. The Following User Says Thank You to Schmoopy For This Useful Post:

    ntin0s (11-20-2010)

  10. #18
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    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.";

    }
    ?>

  11. #19
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    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($str0$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;
    }
    ?>

  12. The Following User Says Thank You to Schmoopy For This Useful Post:

    ntin0s (11-20-2010)

  13. #20
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    Thats perfect!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •