taith Posted November 17, 2006 Share Posted November 17, 2006 this code works all well and good... however, for some reason str_replace() is being case specific. just wonding if anybody has any ideas why/how to fix that...[code]<?function filter_censor($string,$badwords=array()){ foreach($badwords as $badword){ if(strpos($string, $badword)){ for($i=0;$i<strlen($badword);$i++) $x .= "*"; $censor = str_replace($badword, $x, $string); } } return $censor;}$badword[]="hello";echo filter_censor("Hello",$badword);echo filter_censor("hello",$badword);?>[/code] Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 17, 2006 Share Posted November 17, 2006 It is NOT being case insensitive. Did you not notice that you were only getting 1 response printed to the page?Look at your function again. If the conditional is not set "if(strpos($string, $badword))" then you are not setting $censor. In other words, if no bad words exist in the string NOTHING gets returned at all.You need to change this[code]<?phpfunction filter_censor($string,$badwords=array()){ foreach($badwords as $badword){ if(strpos($string, $badword)){ for($i=0;$i<strlen($badword);$i++) $x .= "*"; $censor = str_replace($badword, $x, $string); } } return $censor;}?>[/code]To this[code]<?phpfunction filter_censor($string,$badwords=array()){ foreach($badwords as $badword){ if(strpos($string, $badword)){ for($i=0;$i<strlen($badword);$i++) $x .= "*"; $string = str_replace($badword, $x, $string); } } return $string;}?>[/code]You need to do that anyway or the script wouldn't work with mutiple bad words since the 2nd time through the loop it would be acting ont he original text not the text processed after the last time through. Quote Link to comment Share on other sites More sharing options...
taith Posted November 17, 2006 Author Share Posted November 17, 2006 at first glance... yes, i did make an oopsie... however, before it was blocking one of my words... now it isnt blocking either... Quote Link to comment Share on other sites More sharing options...
taith Posted November 17, 2006 Author Share Posted November 17, 2006 ok... got that fixed... however it is still only blocking 1 word[code]<?function filter_censor($censor,$badwords=array()){ foreach($badwords as $badword){ if(strpos($censor, $badword)){ for($i=0;$i<strlen($badword);$i++) $x .= "*"; $censor = str_replace($badword, $x, $censor); } } return $censor;}$badword[]="hello";echo filter_censor("Hello hello",$badword);?>[/code] Quote Link to comment Share on other sites More sharing options...
taith Posted November 17, 2006 Author Share Posted November 17, 2006 okie :-D is fixed and works perfectly :-Danyone that wants to use it... feel free :-P[code]<?function filter_censor($censor,$badwords=array()){ foreach($badwords as $badword){ for($i=0;$i<strlen($badword);$i++) $x .= "*"; $censor = str_ireplace($badword, $x, $censor); unset($x); } return $censor;}$badword[]="hello";echo filter_censor("Hello hello",$badword);?>[/code] Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 17, 2006 Share Posted November 17, 2006 No offense, but you could accomplish the same thing with a regular expression replace command. I'm not RegEx expert, but I'm sure you could find one with a little searching. Quote Link to comment Share on other sites More sharing options...
taith Posted November 17, 2006 Author Share Posted November 17, 2006 true... however this one is much easier to connect with a mysql database... :-) Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 17, 2006 Share Posted November 17, 2006 That has nothing to do with your script. You could just as easlity take the bad words from the database and utilize them in a regular expression command to do what you are doing with loops.One tweak you could make to imporove efficiency would be to get rid of the loop to create the replacement word, like this:[code]<?phpfunction filter_censor($censor,$badwords=array()){ foreach($badwords as $badword){ $censor = str_ireplace($badword, str_pad("",strlen($badword),"*");, $censor); } return $censor;}$badword[]="hello";echo filter_censor("Hello hello",$badword);?>?>[/code] Quote Link to comment Share on other sites More sharing options...
taith Posted November 18, 2006 Author Share Posted November 18, 2006 sweet deal :-) thanks... and you had an extra ";"[code]<?phpfunction filter_censor($censor,$badwords=array()){ foreach($badwords as $badword){ $censor = str_ireplace($badword, str_pad("",strlen($badword),"*"), $censor); } return $censor;}$badword[]="hello";echo filter_censor("Hello hello",$badword);?>[/code] 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.