Jump to content

[SOLVED] Word filter [replace whole word instead of part]


pocobueno1388

Recommended Posts

I have a bad word filter right now and the problem is that it replaces a bad word inside of a word.

 

For example: Say I typed "Grass". The new word would come out as Gr[censored], as you can see there is a bad word at the end.

 

I only want it looking for the specific words I have in the filter, instead of replacing bad words inside of words.

 

Here is the code to what I currently have:

 


<?php

function filterbadwords($str){

// words to filter

$badwords=array( "word1", "word2", "word3");

// replace filtered words with

$replacements=array( "<font color=red>[Censored]</font>");

for($i=0;$i < sizeof($badwords);$i++){
  $rand_key = (rand()%sizeof($replacements));
  $str=eregi_replace($badwords[$i], $replacements[$rand_key], $str);
  }
  
return $str;
}


?>

 

How could I change this to do what I want. I appreciate all your help :) Thank you.

Link to comment
Share on other sites

I hate to say it, but that post didn't help me much either =/ I tried everything I saw on the page and came back with the same result each time. I even searched google again and again and tried every tutorial filter, and they come out with the same results as well.

 

Wow...this is going to be a challenge. Any more ideas?

Link to comment
Share on other sites

how about this:

 

function filterbadwords($str){

// words to filter

$badwords=array("ass", "bitch", "word3");

// replace filtered words with

$replacements=array("<font color=red>[Censored]</font>");
$words = explode(" ", $str);
$out_words = "";

for($i = 0; $i < count($words); $i++){
if(in_array(str_replace(".", "", $words[$i]), $badwords)){
$out_words[] = $replacements[0];
}else{
$out_words[] = $words[$i];
}
  }
  
return implode(" ", $out_words);
}

Link to comment
Share on other sites

Ahh I had to think about this one a moment and then realized how simple it is.

 

<?php
$string = "Corbin is awesome, and awesome is CoRbIn.";
$bad[] = "/\bcorbin\b/i";
$bad[] = "/\bawesome\b/i";
$good = "[censored]";
$string = preg_replace($bad, $good, $string);
echo $string;
?>

 

That would return "[censored] is [censored], and [censored] is [censored]."

 

\b signifies a word boundry so that it must be a lone word, and /i makes it case-insensitive

 

The replacement variable is just a plain thing, but you could build an array corresponding to the words to be replaced such as:

$good[] = "c*rbin";

$good[] = "awes***"";

 

Someone posted before me, and I don't know if theirs works or not, but mine will probably help too :P .

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.