bradgrafelman;10879896 wrote:One change I would suggest is that you utilize preg_replace()'s 5th parameter and introduce a "count" variable. Use this to gauge whether $profanityStatus should be set to true (i.e. if the count is >0, then some profanity was found). Otherwise, legitimate text can lead to a false positive if the user wanted to use an asterisk in his/her message.
Ah, nice suggestion Brad! I want that! Unfortunatelty, I could not get it working.. I looked at the manual here:
http://us.php.net/manual/en/function.preg-replace.php
and emulated this snippet:
$count = 0;
echo preg_replace(array('/\d/', '/\s/'), '*', 'xp 4 to', -1 , $count);
echo $count; //3
Of course, I used my previous example preg_replace instead of the sample shown here.. I had a $count=0; line just above, and I inserted -1, $count as the 4th and 5th variables and I had echo $count just after the preg (just like the snippet),, the $count number kept reporting 0.. so I tried passing it by reference (&count), I even tried ++$count.. (I just could not get the count variable to record properly.. 0 count it seems 🙁 If I can get help on that one thing.. it will all come together beautifully..
bradgrafelman;10879896 wrote:Also, you mentioned in the inline comment that if there was profanity found then the entire form would be nullified and no e-mail sent. What, then, is the point of replacing the profanity when it's found? Why not just search for it and, if found, halt the process there rather than have PHP do some find-and-replace, additional censoring, and then finally die?
Ok, change of plan here.. the idea was to get the users to 'fix' the message by clearing the asterisks themselves.. but after your comment, I re-thought it.. so now, I'll allow the astersisks to pass along in the message.. I'll see who swore and where 🙂 so now there is no inconvenience to the users in this regard.
so now my code is as follows:
$badWord = array('badWord-01','badWord-02','badWord-03','badWord-04','badWord-05','badWord-06','badWord-07','badWord-08','badWord-09','badWord-10');
$censor = array('f--k','sh-t'); // actually swear words in real script.
// first remove any potential html / scripting tags...
$_POST['textarea'] = preg_replace('#<[^>]+>#' , '' , $_POST['textarea']);
// now check and sensor out any profanity...
$profanityStatus = false; // innocent till proven guilty...
foreach($badWord as $remove){ // first begin by checking for complete words that match complete profanity words...
$_POST['textarea'] = preg_replace('#\b'.$remove.'\b#i' , '*' , $_POST['textarea']);
}
// and then check for words that are partial in profanity... example: f--ker, f--king or f--k-adoodaaday
$_POST['textarea'] = str_ireplace($censor, '*', $_POST['textarea']);
if(preg_match('/[*]/', $_POST['textarea'])){
$profanityStatus = true; // if I can get that 5th $count variable working in preg, I can convert str_ireplace to preg as well and once again check for this $count variable and if it is still 0, profanityStatus = false...
}
// finally, remove any added slahes due to get_magic_quotes_gpc()...
if(get_magic_quotes_gpc()){
$_POST['textarea'] = stripslashes($_POST['textarea']);
}
It is pretty solid now.. I played around with it for a while.. it is by no means perfect of course.. as I don't think I can code against EVERYTHING.. but in anycase, go ahead.. amuse yourself 😃
So Brad, if you can show me what I am doing wrong with regards to that 5th $count variable in preg_replace, that would be much appreciated!
Thanks for the feedback so far! Much appreciated.
Cheers,
NRG