JonnySnip3r Posted August 2, 2010 Share Posted August 2, 2010 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! Quote Link to comment Share on other sites More sharing options...
lemmin Posted August 2, 2010 Share Posted August 2, 2010 I think all of the POSIX functions were deprecated. You might want to consider using the PCRE ones. You might have to use preg_match() to count the number found and then search again to replace them. Quote Link to comment Share on other sites More sharing options...
radar Posted August 2, 2010 Share Posted August 2, 2010 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. Quote Link to comment Share on other sites More sharing options...
radar Posted August 2, 2010 Share Posted August 2, 2010 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 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.