0

I am trying to create an array that include insulting words and when someone tries to submit any of the words that are inside the array, he/she will get an error.

I've tried a couple of time, but failed!

Can anyone help me out please? :)

$censor_ary = array('word1', 'word2', 'word3');

    foreach ($censor_ary as $censor)
    {
        $word = $censor;    
    }

if ($_POST['mesazhi'] == $word)
        {
            echo '<span>P&euml;rdorimi i fjal&euml;ve fyese nuk &euml;sht&euml; e mir&euml;seardhur</span>';
        }
skaffman
  • 398,947
  • 96
  • 818
  • 769
aborted
  • 4,481
  • 14
  • 69
  • 132
  • 4
    This is very hard task to do correctly. For a good read: http://www.codinghorror.com/blog/2008/10/obscenity-filters-bad-idea-or-incredibly-intercoursing-bad-idea.html – Rob Hruska Jan 29 '11 at 20:54
  • 1
    If you've tried, then post your code, it might be close to the solution. – greg0ire Jan 29 '11 at 20:54
  • "I've tried a couple of times, but failed!"... What was the fail? Array creation? Comparing a potential insulting word with your array? – chahuistle Jan 29 '11 at 20:55
  • The idea would be to iterate over the array with words, and use a regex to test each and every word. Or make 1 big regexp with all words (/foo|bar|baz/i). – Harmen Jan 29 '11 at 20:56
  • possible duplicate of [How do you implement a good profanity filter?](http://stackoverflow.com/questions/273516/how-do-you-implement-a-good-profanity-filter) – moinudin Jan 29 '11 at 20:56

4 Answers4

3
$badWords = array(
     'bad' => '***',
     'badly' => '***');

strtr("This is a bad sentence", $badWords); // This is a *** sentence

You can create an array which contains bad words and cleaned versions (or just asterix **) . And then, you can use strtr() for filtering.

Eray
  • 7,038
  • 16
  • 70
  • 120
2

Here is it:

$words = array('word1', 'word2', 'word3', '...');
$re_words = array();
foreach($words as $word) $re_words[] = preg_quote($word, '#');

if (preg_match('#\b(' . implode('|', $re_words) . ')\b#i', $post, $word) {
    // error, the $post contains the word $word[0]
}

This will detect any word listed in the $words array.

Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
1

Simple, loop through the array with foreach statement, and use preg_match to check if the word is included in the submitted $_POST variable (I am assuming)

Or something like this:

$arr = array('word1','word2');

foreach ($arr as $word)
{
if (preg_match("$word",$data))
{
//error here
}
}
cyber-guard
  • 1,776
  • 14
  • 30
0

You can try this code that has worked on my website. Replace all variables called your variable here with your variable. You need a CSV file that contains the expletives. This code is able to distinguish between a swear word and an inocent word containing a swear word, for example Scunthorpe. It also replaces the word with the appropriate number of stars, and will recognise all common suffixes. It can take a while to run, but significantly reduces the risk of false positives.

//inport profanities csv and list suffixes
$profanities=explode(",", file_get_contents('NAME OF YOUR CSV FILE GOES HERE'));

$suffixes=array('','s','es','e','ed','ing','ted','ting','y','ty','d','head','bag','hole','wit','tard','er','ter','en','ten','est','test','able','ible','ful','full');

//get text input
$sanitize_text=$YOUR VARIABLE HERE;

//combine profanities and sufixes
foreach($profanities as $profanity)
{
foreach($suffixes as $suffix)
{
$sanitize_terms=$profanity;
$sanitize_terms.=$suffix;
$word=$sanitize_terms;

$match_count=preg_match_all('/'.$word.'/i', $YOUR VARIABLE HERE, $matches);
for($i=0; $i<$match_count; $i++)
{
$bwstr=trim($matches[0][$i]);
$sanitize_text=preg_replace('/\b'.$bwstr.'\b/', str_repeat("*", strlen($bwstr)), $sanitize_text);
}
}
}


$YOUR VARIABLE HERE=$sanitize_text;