Jump to content

censor script


taith

Recommended Posts

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
Share on other sites

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]<?php
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;
}
?>[/code]

To this
[code]<?php
function 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
Share on other sites

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
Share on other sites

okie :-D is fixed and works perfectly :-D
anyone 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
Share on other sites

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]
<?php
function 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
Share on other sites

sweet deal :-) thanks... and you had an extra ";"

[code]
<?php
function 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
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.