PHP swear word filter

后端 未结 2 1411
栀梦
栀梦 2020-12-11 04:08

I\'m working on a WordPress plugin that replaces the bad words from the comments with random new ones from a list.

I now have 2 arrays: one containing the bad words

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-11 04:41

    I came up to this method and it's working fine. Returning true, in case there is an entry of bad words in the entry.

    Example:

    function badWordsFilter($inputWord) {
      $badWords = Array("bad","words","here");
      for($i=0;$i

    Usage:

    if (badWordsFilter("bad")) {
        echo "Bad word was found";
    } else {
        echo "No bad words detected";
    }
    

    As the word 'bad' is blacklisted it will echo.

    Online example 1

    EDIT 1:

    As offered by rid it's also possible to do simple in_array check:

    function badWordsFilter($inputWord) {
      $badWords = Array("bad","words","here");
         if(in_array(strtolower($inputWord), $badWords) ) {
            return true;
         }
      return false;
    }
    

    Online example 2

    EDIT 2:

    As I promised, I came up to the slightly different idea of replacing bad words with good words, as you mentioned in your question. I hope it will help you a bit but this is the best I can offer at the moment, as I'm totally not sure on what you're trying to do.

    Example:

    1. Let's combine an array with bad and good words into one

    $wordsTransform = array(
      'shit' => 'ship'
    );
    

    2. Your imaginary user input

    $string = "Rolling In The Deep by Adel\n
    \n
    There's a fire starting in my heart\n
    Reaching a fever pitch, and it's bringing me out the dark\n
    Finally I can see you crystal clear\n
    Go ahead and sell me out and I'll lay your shit bare";
    

    3. Replacing bad words with good words

    $string = strtr($string, $wordsTransform);
    

    4. Getting the desired output

    Rolling In The Deep

    There's a fire starting in my heart
    Reaching a fever pitch, and it's bringing me out the dark
    Finally I can see you crystal clear
    Go ahead and sell me out and I'll lay your ship bare

    Online example 3

    EDIT 3:

    To follow the correct comment from Wrikken, I have totally forgotten about that strtr is case sensitive and that it's better to follow word-boundary. I have borrowed the following example from
    PHP: strtr - Manual and modified it slightly.

    Same idea as in my second edit but not register dependent, it checks for word boundaries and puts a backslash in front of every character that is part of the regular expression syntax:

    1. Method:

    //
    // Written by Patrick Rauchfuss
    class String
    {
        public static function stritr(&$string, $from, $to = NULL)
        {
            if(is_string($from))
                $string = preg_replace("/\b{$from}\b/i", $to, $string);
    
            else if(is_array($from))
            {
                foreach ($from as $key => $val)
                    self::stritr($string, $key, $val);
            }
            return preg_quote($string); // return and add a backslash to special characters
        }
    }
    

    2. An array with bad and good words

    $wordsTransform = array(
                'shit' => 'ship'
            );
    

    3. Replacement

    String::stritr($string, $wordsTransform);
    

    Online example 4

提交回复
热议问题