user3449212
user3449212

Reputation:

filtering bad words from text

This function filer the email from text and return matched pattern

  function parse($text, $words)
  {
    $resultSet = array();
    foreach ($words as $word){
      $pattern = 'regex to match emails';
      preg_match_all($pattern, $text, $matches, PREG_OFFSET_CAPTURE );
      $this->pushToResultSet($matches);
    }
    return $resultSet;
  }

Similar way I want to match bad words from text and return them as $resultSet.

Here is code to filter badwords

TEST HERE

$badwords = array('shit', 'fuck'); // Here we can use all bad words from database
$text = 'Man, I shot this f*ck, sh/t! fucking fu*ker sh!t f*cking  sh\t ;)';
echo "filtered words <br>";
echo $text."<br/>";
$words = explode(' ', $text);
foreach ($words as $word)
    {
        $bad= false;
        foreach ($badwords as $badword)
            {
                if (strlen($word) >= strlen($badword))
                {
                    $wordOk = false;
                    for ($i = 0; $i < strlen($badword); $i++)
                    {   
                        if ($badword[$i] !== $word[$i] && ctype_alpha($word[$i]))
                        {
                            $wordOk = true;
                            break;
                        }
                    }
                    if (!$wordOk)
                    {
                        $bad= true;
                        break;
                    }
        }
            }   
            echo $bad ? 'beep ' : ($word . ' '); // Here $bad words can be returned and replace with *. 
    }

Which replaces badwords with beep

But I want to push matched bad words to $this->pushToResultSet() and returning as in first code of email filtering.

can I do this with my bad filtering code?

Upvotes: 3

Views: 1739

Answers (2)

TMH
TMH

Reputation: 6246

Roughly converting David Atchley's answer to PHP, does this work as you want it to?

$blocked = array('fuck','shit','damn','hell','ass');
$text = 'Man, I shot this f*ck, damn sh/t! fucking fu*ker sh!t f*cking  sh\t ;)';
$matched = preg_match_all("/(".implode('|', $blocked).")/i", $text, $matches);
$filter = preg_replace("/(".implode('|', $blocked).")/i", 'beep', $text);
var_dump($filter);
var_dump($matches);

Upvotes: 1

David Atchley
David Atchley

Reputation: 1204

JSFiddle for working example.

Yes, you can match bad words (saving for later), replace them in the text and build the regex dynamically based on an array of bad words you're trying to filter (you might store it in DB, load from JSON, etc.). Here's the main portion of the working example:

var blocked = ['fuck','shit','damn','hell','ass'],
    matchBlocked = new RegExp("("+blocked.join('|')+")", 'gi'),
    text = $('.unfiltered').text(),
    matched = text.match(matchBlocked),
    filtered = text.replace(matchBlocked, 'beep');

Please see the JSFiddle link above for the full working example.

Upvotes: 0

Related Questions