Jump to content

php function help (bad_word_cleanser)


JonnySnip3r

Recommended Posts

Hey guys i have a bad word cleanser function:

 

function bad_word_cleanser($string){
	// Words to filter
	$bad_words = array("blast", "bitch");

	// Replace words with
	$clean_words = array("[apple]","[banana]");

	for($i = 0; $i < sizeof($bad_words); $i++){
		srand((double)microtime()*1000000);
		$rand_key = (rand()%sizeof($clean_words));
		$string = eregi_replace($bad_words[$i], $clean_words[$rand_key], $string);
	}

	return $string;
}

 

it checks to see if any bad words are used and then chooses a random clean word to replace it with. any suggestions on how i could have this pick up the amount of words it has used and then have the ability to email the user if more than say 6 words are used. i have tried i just cant get it to work. Hope someone can help.

 

Thanks!

Link to comment
Share on other sites

function filterBadWords($str){

 

// words to filter

$badwords=array( "[naughty word removed]", "[naughty word removed]", "[no swearing please]", "[oops]", "[oops]", "[naughty word removed]", "[oops]", "[oops]" );

 

// replace filtered words with

$replacements=array( "[naughty word removed]", "[how wude!]", "[no swearing please]" );

 

for($i=0;$i < sizeof($badwords);$i++){

  srand((double)microtime()*1000000);

  $rand_key = (rand()%sizeof($replacements));

  $str=eregi_replace($badwords[$i], $replacements[$rand_key], $str);

}

return $str;

}

 

example call : $your_text_on_page = filterBadWords($your_text_from_comments);

 

that example works for me.. unsure why it wouldnt work for you.

Link to comment
Share on other sites

another option is this:

 

// Filtering Function

function filterBadWords($str,$badWordsFile) {
  $badFlag = 0;
  if(!is_file($badWordsFile)) {
    echo "ERROR: file missing: ".$badWordsFile;
    exit;
  }
  else {
    $badWordsFH = fopen($badWordsFile,"r");
    $badWordsArray = explode("\n", fread($badWordsFH, filesize($badWordsFile)));
    fclose($badWordsFH);
  }
  foreach ($badWordsArray as $badWord) {
    if(!$badWord) continue; 
    else {
      $regexp = "/\b".$badWord."\b/i";
      if(preg_match($regexp,$str)) $badFlag = 1;
    }
  }
    if(preg_match("/\[url/",$str)) $badFlag = 1;
  return $badFlag;
}

 

// Function Call/Usage
if (filterBadWords($message,"badwords.txt") == 0) {
  mail("mail@destination.com", $subject, $message, $from);
}
header("Location: http://www.siteurl.com/index.php?p=Thank_You");

 

// badwords.txt
word1
word2
word3

 

and of course if that one doesnt work for you there is another option here:

 

http://codingforums.com/showthread.php?t=135849

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.