pocobueno1388 Posted February 4, 2007 Share Posted February 4, 2007 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. Quote Link to comment Share on other sites More sharing options...
fert Posted February 4, 2007 Share Posted February 4, 2007 $badwords=array( " word1 ", " word2 ", " word3 "); Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted February 4, 2007 Author Share Posted February 4, 2007 Nope, that didn't work. I got all excited when I saw it because I thought I was an idiot for not thinking of that...but it doesn't work =( Any other ideas? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 4, 2007 Share Posted February 4, 2007 See this topic: http://www.phpfreaks.com/forums/index.php/topic,124570.0.html Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted February 4, 2007 Author Share Posted February 4, 2007 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? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 4, 2007 Share Posted February 4, 2007 If you tried many different things and got the same result for every one, there might be another problem. I will write a function to do what I described, and post back momentarily. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted February 4, 2007 Share Posted February 4, 2007 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); } Quote Link to comment Share on other sites More sharing options...
corbin Posted February 4, 2007 Share Posted February 4, 2007 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 . Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted February 4, 2007 Author Share Posted February 4, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.