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] Link to comment https://forums.phpfreaks.com/topic/27580-censor-script/ 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. Link to comment https://forums.phpfreaks.com/topic/27580-censor-script/#findComment-126159 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... Link to comment https://forums.phpfreaks.com/topic/27580-censor-script/#findComment-126163 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] Link to comment https://forums.phpfreaks.com/topic/27580-censor-script/#findComment-126164 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] Link to comment https://forums.phpfreaks.com/topic/27580-censor-script/#findComment-126168 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. Link to comment https://forums.phpfreaks.com/topic/27580-censor-script/#findComment-126207 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... :-) Link to comment https://forums.phpfreaks.com/topic/27580-censor-script/#findComment-126210 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] Link to comment https://forums.phpfreaks.com/topic/27580-censor-script/#findComment-126342 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] Link to comment https://forums.phpfreaks.com/topic/27580-censor-script/#findComment-126603 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.