Page 1 of 3 123 LastLast
Results 1 to 10 of 25

Thread: Need a very simple word filter

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

    Default Need a very simple word filter

    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
    Last edited by ntin0s; 11-17-2010 at 06:19 PM.

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

    Default

    Without a great knowledge of regexp I'd do it this way:

    PHP Code:
    // Input string ($_REQUEST['msg'] in your case)
    $str 'echo Hello';

    // Get the first 4 characters of the string
    $sub substr($str04);

    // If it matches then echo out the latter part of the string
    if($sub == 'echo')
        echo 
    substr($str4);

    // 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.

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

    ntin0s (11-20-2010)

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

    Default

    It's not working. I just need the word echo not to be displayed if is written

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

    Default

    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.

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

    ntin0s (11-20-2010)

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

    Default

    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

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

    Default

    Oh right, I was just using $str as an example, this should work for you:

    PHP Code:
    $str $_REQUEST['msg'];

    // Get the first 4 characters of the string
    $sub substr($str04);

    // If it matches then echo out the latter part of the string
    if($sub == 'echo')
        echo 
    substr($str4); 

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

    ntin0s (11-20-2010)

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

    Default

    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.

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

    Default

    What do you want it to say if "echo" is not at the beginning?

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

    Default

    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:

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

    Default

    PHP Code:
    <?php

    $str 
    $_REQUEST['msg'];

    // Get the first 4 characters of the string
    $sub substr($str04);

    // If it matches then echo out the latter part of the string
    if($sub == 'echo')
        echo 
    substr($str4);
    else
        echo 
    $str;
        
    ?>

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

    ntin0s (11-20-2010)

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
  •