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.

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?

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);
}

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 .

ProjectFear gave me exactly what I wanted xP Thank you so much. Corbin, I didn't event est your code, but I am sure it would have probably worked as well. So thank you both for your time :) And Jesirose of course.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.