3

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?

  • [Related](http://stackoverflow.com/q/273516/1578604) – Jerry May 26 '14 at 06:26
  • Let me share [this gem](http://stackoverflow.com/questions/6096634/regular-expression-preg-quote-symbols-are-not-detected/6099598#6099598) with you. – georg Jun 03 '14 at 12:32

2 Answers2

1

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);
TMH
  • 6,096
  • 7
  • 51
  • 88
0

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.

David Atchley
  • 1,204
  • 8
  • 10