Page 1 of 1
Removing or blocking Bad Language
Posted: Sat May 22, 2010 10:00 am
by simonmlewis
Code: Select all
$communication=$_POST['communication'];
$communication = stripslashes(preg_replace('#^(.*?)((\d{4}[ -]{0,1})(\d{4}[ -]{0,1})(\d{4}[ -]{0,1})(\d{4}[ -]{0,1}))(.*)$#', '${1}************${6}${7}', preg_quote($communication)));
$words = array('bad1','bad2', bad3');
$commfixed = str_ireplace($words,'**removed**',$communication);
I want to be able to stop a post of a form if it has a particular swear word in it.
The code above will do it and put a **removed** in its place, but I want to be able to STOP the email in its tracks. And potentially redirect them to the form telling them to "mind their language".
Am having trouble finding out how to say if $words is in $commfixed then to REDIRECT. I know the redirect code, just cannot grasp how to do it the moment it find a word.
Help??
Re: Removing or blocking Bad Language
Posted: Sat May 22, 2010 11:35 am
by AbraCadaver
The easiest using your existing code would be this:
Code: Select all
if($commfixed != stripslashes($communication)) {
// bad words, redirect
}
// or
if(strpos($commfixed, '**removed**') !== false) {
// bad words, redirect
}
Re: Removing or blocking Bad Language
Posted: Sat May 22, 2010 11:56 am
by lcarron000
Code: Select all
<?php
$communication = $_POST['communication'];
$communication = stripslashes(preg_replace('#^(.*?)((\d{4}[ -]{0,1})(\d{4}[ -]{0,1})(\d{4}[ -]{0,1})(\d{4}[ -]{0,1}))(.*)$#',
'${1}************${6}${7}', preg_quote($communication)));
$words = array('bad1', 'bad2', 'bad3'); //Fixed missing single quote before < bad3 >
$replaceWith = "RemovedBadWord";
$commfixed = str_ireplace($words, $replaceWith, $communication); // changed <**removed**> to <RemovedBadWord>
if (preg_match("/\bRemovedBadWord\b/i", $commfixed)) { // search the string and single out <RemovedBadWord>. If you have a match set $match to true.
$match = true;
} else { //If not set $match to false
$match = false;
}
if ($match == true) { //If there is a match ...
header("Location: url here"); //Go to this page
}
?>