Removing or blocking Bad Language

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Removing or blocking Bad Language

Post 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??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Removing or blocking Bad Language

Post 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
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
lcarron000
Forum Newbie
Posts: 13
Joined: Thu May 20, 2010 2:51 pm

Re: Removing or blocking Bad Language

Post 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
}
?>
Post Reply