Help with script to censor words

Discussion in 'PHP' started by Phil Vessey, May 6, 2007.

  1. #1
    Hi gang

    I would appreciate some help with the censor words script that I am attempting to create. So far I have managed to create this.

    
     
    $str = "This is an assessment";
     
    $banned_words = "ass|asswipe"
    $censor_words = explode("|", $banned_words);
    foreach ($censor_words as $word) {
    $str = preg_replace("/\b($word)\b/", str_repeat("*", strlen($word)), $str);
    }
     
    
    PHP:
    The way I have this at the moment, it will censor "ass" and "asswipe" providing that they are used on their own and not part of another word. How would I change this so that it will also censor them when they are part of another word like "assessment" would appear as "***essment".

    Thanks
    Phil
     
    Phil Vessey, May 6, 2007 IP
  2. KalvinB

    KalvinB Peon

    Messages:
    2,787
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #2
    "/$word/ig"

    "i" makes it case insensitive and "g" makes it greedy so all instances are replaced.

    This is generally a futile excercise as there are a million and one ways to be vulgar.
     
    KalvinB, May 6, 2007 IP
  3. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $str = preg_replace("/\b($word)\b/", str_repeat("*", strlen($word)), $str);

    Remove the \b from the regex - \b matches a word boundary.

    You probably do want to go case-insensitive but you don't need the g modifier - in terms of regex, greedy/ungreedy relates to quantifiers, not the number of replacements to make. By default, when you use a +, * or ? symbol, it will match as much as possible (hence greedy).
     
    rodney88, May 6, 2007 IP
  4. Phil Vessey

    Phil Vessey Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for the responce thats help a lot. How do you add 2 checks in the regex function? Say if i wanted to use \b and the \i for case-insensitive. would it be like this "/\b\i($word)\b\i/" or something else?
     
    Phil Vessey, May 6, 2007 IP
  5. Phil Vessey

    Phil Vessey Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ahh I eventually found it on the PHP website hidden away in the function manuals. \b will do the boundries and (?i) will do the case-insensitive. Thanks for the help.
     
    Phil Vessey, May 6, 2007 IP
  6. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #6
    \b is part of the pattern itself and goes where needed. i is a pattern modifier that goes after the delimiter and applies to the whole pattern.

    "/\b($word)\b/i"
     
    rodney88, May 6, 2007 IP