PDA

View Full Version : Newbie Needs Help w. Censor/Profanity Filter



zenmaster
29 Jan 2006, 09:14 PM
Hi,

I'm a complete newbie to PHP, so I hope you won't laugh at me too much. I'm trying to implement a censor/profanity filter for my "Tell-A-Friend" form on my site and I've searched this forum and others for some examples and came across this one (it's named 'censor.php') that uses a separate file called censorwords.txt:


<?php
function censor($content){
$words_list=@file('censorwords.txt');
$search=array('a','b','i','l','o','p','s');
$replace=array('(?:a|\@|\*)','(?:b|8|3|\*)','(?:i|1|l|\!|\*)','(?:l|1|i|\!|\*)','(?:o|0|\*)','(?:p|\ ?|\*)','(?:s|\$|\*)');
foreach($words_list as $badword){
$badword=rtrim($badword);
$len=strlen($badword);
$rep='';
for($i=0; $i < $len; $i++){
$rep.=' ';
}
$badwordpreg=preg_split('//', $badword, -1, PREG_SPLIT_NO_EMPTY);
$badwordpreg=str_replace($search, $replace, $badwordpreg);
$badword='';
for($i=0; $i < count($badwordpreg); $i++){
$badword.=$badwordpreg[$i];
if($i != (count($badwordpreg)-1)) $badword.='(.{0,5})';
}
$badword="/$badword/i";
$content=preg_replace($badword, $rep, $content);
}
return $content;
}
?>


I understand that I place a list of the words that I want to censor in the censorwords.txt file but my question is, since I'm completely new to PHP, exactly where do I insert the above code into my existing form below (the 'textarea' is what I want to censor)? I presume it involves inserting the '$content' code somewhere in the form along with calling up the censor.php file somewhere, right?

(This form actually sits inside a larger 'form.php' page that contains the entire "Tell-A-Friend" script):

<form method="post" name="pollform" action="<?=$_SERVER['PHP_SELF'] ?>">

<?php } ?>
<p><strong>Your Name: </strong><br /><input type="text" name="userName" value="<?=$userName ?>" size="22"></p>
<p><strong>Your Email: </strong><br /><input type="text" name="userEmail" value="<?=$userEmail ?>" size="22"></p>
<p><strong>Your Friend’s Email: </strong><br /><input type="text" name="reEmail" value="<?=$reEmail ?>" size="22"></p>
<?php if($allowMessage == "Yes") {
?>
<?php } ?>
<p><strong>Your Comments: </strong><br /><textarea name="userMessage" wrap="physical" cols="24" rows="6"><?=$userMessage ?></textarea>
<input type="hidden" name="action" value="send_message">
<input type="submit" name="formsub" value=" Send ">

</form>


Sorry if this comes across as completely ignorant, but any help is greatly appreciated. Thanks.