Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Feb 2003
    Posts
    345
    Thanks
    0
    Thanked 0 Times in 0 Posts

    function to filter rude-words, please

    I want to include a function in a page to filter all rude words submited by users. The filter should be something like (i.e.):

    Words beginning by "abc", should be replaced with "****" or "oops" or whatever, so having an array like (i.e.):

    $badwords = array(abc,aei,xyz);

    How would the function look like to replace "abcde" and "aeiou" to "oops" when the input is (i.e.):

    $text = "Hello abcde, what's up?. Your bro is an aeiou. Byeee"

    So the output would be: "Hello oops, what's up?. Your bro is an oops. Byeee"

    Is that clear? Thanks gurus.
    Last edited by maltrecho; 11-07-2003 at 12:43 PM.

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    If you need to simply replace words in a string there are functions for that

    http://be.php.net/manual/en/function.str-replace.php


    if you need to do something a litte more complex then use REGEX

    http://be.php.net/manual/en/function.ereg-replace.php
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Regular Coder
    Join Date
    Feb 2003
    Posts
    345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The problem is that I need to replace all the words beginning with (i.e.) "foo","fee"... not just full words like "food","feet"... and I don't have a clue about how to write a regexp like that.

    This is what I've got so far:
    PHP Code:
    <?php
    $text 
    "while footing, I broke my feet";
    echo 
    str_replace(array("foo","fee"), "oops"$text);
    ?>
    The output is "while oopsting, I broke my oopst", but I want to have "while oops I broke my oops"
    Last edited by maltrecho; 11-07-2003 at 01:22 PM.

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You mean like this?

    PHP Code:
    $text "while footing, I broke my feet";

    echo 
    preg_replace(array("/foo.*?\b/i""/fee.*?\b/i"), "oops"$text); 
    De gustibus non est disputandum.

  • #5
    Regular Coder
    Join Date
    Feb 2003
    Posts
    345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's giving me while oopsroke my feet an it's really a pity because I just can't understand anything about reg exp to try new things.

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, the forum software ate my backslashes. I should have known better... anyway, try this again:

    Code:
    $text = "while footing, I broke my feet";
    
    echo preg_replace(array("/foo.*?\b/i", "/fee.*?\b/i"), "oops", $text);
    De gustibus non est disputandum.

  • #7
    Regular Coder
    Join Date
    Feb 2003
    Posts
    345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wow! That's great. From the deep


  •  

    Posting Permissions

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