Jump to content

Word Filter arrays...


xyn

Recommended Posts

Hi Guys,

I was wondering how to add a word filter to my e-mail support form, say I had the following fields:
Name
Email
Message

How could I make a bad word filter to prevent swearing being applied into the "Name, e-mail and message" fields?

thanks.
Link to comment
Share on other sites

[code=php:0]//Lets say $text is the variable you want to scan for bad words
$bad_words=array(...); //Put all the bad words this way: array(word1,word2,...)
foreach($bad_words as $word){
if(strpos($text,$word)!==FALSE){die("Error- please dont use the word".$word);};
};[/code][hr]

Orio.
Link to comment
Share on other sites

You can, but I dont see that helpful  ???
If you still want:

[hr][code=php:0]//Lets say $text is the variable you want to scan for bad words

//connect to db here

$query="SELECT badwords FROM badwords_table";
$result=mysql_query($query);

while($word=mysql_fetch_array($result)){
if(strpos($text,$word["badwords"])!==FALSE){die("Error- please dont use the word".$word);};
};[/code][hr]

Orio.
Link to comment
Share on other sites

[quote author=xyn link=topic=98993.msg389637#msg389637 date=1151672751]
Thanks for the responces.

But Is there anyway I can make a bad word list in MYSQL and extract each word into the array();
[/quote]

yes, that's one thing i like to do with my comment systems... i like to have a table with bad words and replacements, then, i simply run a function like this to clean the posts:
[code]
<?php
// you must make your connection before running this function
function filterWords($String) {
  $sql = mysql_query("SELECT * FROM wordsTable");
  $words = array();
  $replace = array();
  while ($x = mysql_fetch_array($sql)) {
    $words[] = $x['badWord'];
    $replace[] = $x['replacement'];
  }

  return str_replace($words, $replace, $String);
}
?>
[/code]

hope this helps! this is useful when you want to be able to control your filters from an admin panel since you can easily update your bad words table.
Link to comment
Share on other sites

That was very helpful thanks,

And I'm guessing where you add the words and replacements into the database it would look like
[code=php:0]  $words = array(badword1, etc.);
  $replace = array(replaceword1, etc.);
[/code]
correct?
Link to comment
Share on other sites

[quote author=xyn link=topic=98993.msg389648#msg389648 date=1151673570]
That was very helpful thanks,

And I'm guessing where you add the words and replacements into the database it would look like
[code=php:0]  $words = array(badword1, etc.);
  $replace = array(replaceword1, etc.);
[/code]
correct?
[/quote]

once you pull it out of the database, it is in those arrays, yes. to put new words in, you'd simply run a regular query:
[code]
INSERT INTO wordsTable (badWord, replacement) VALUES ('$myWord', '$myReplacement');
[/code]
Link to comment
Share on other sites

[quote author=obsidian link=topic=98993.msg389650#msg389650 date=1151673918]
[quote author=xyn link=topic=98993.msg389648#msg389648 date=1151673570]
That was very helpful thanks,

And I'm guessing where you add the words and replacements into the database it would look like
[code=php:0]  $words = array(badword1, etc.);
  $replace = array(replaceword1, etc.);
[/code]
correct?
[/quote]

once you pull it out of the database, it is in those arrays, yes. to put new words in, you'd simply run a regular query:
[code]
INSERT INTO wordsTable (badWord, replacement) VALUES ('$myWord', '$myReplacement');
[/code]
[/quote]

Yeah, And I could make a HTML form and use the $_POST[BadWord] & $_POST[Replace];
As well...

Also where do i put the following code?
[code=php:0]<?php
// you must make your connection before running this function
function filterWords($String) {
  $sql = mysql_query("SELECT * FROM wordsTable");
  $words = array();
  $replace = array();
  while ($x = mysql_fetch_array($sql)) {
    $words[] = $x['badWord'];
    $replace[] = $x['replacement'];
  }

  return str_replace($words, $replace, $String);
}
?>
[/code]
Link to comment
Share on other sites

It's a function, so you can put it everywhere you want. Best in the begining/end or in a include file.
And you call it by:
$something=filterWords($string) //when $string is the input (can called in any way, not just $string)

Orio.
Link to comment
Share on other sites

[quote author=xyn link=topic=98993.msg389651#msg389651 date=1151674353]
Also where do i put the following code?
[/quote]

it's just a function, so you need to add it somewhere (doesn't really matter where) on the page it will be used on, and to actually filter your words, all you've got to do is call the function:
[code]
$String = filterWords($String);
[/code]
Link to comment
Share on other sites

hmm I'm slightly confused Now...

If i shoved the function in the top of my page then I just put [code=php:0]$String = filterWords($String);[/code] before the e-mail is actually submitted?
Link to comment
Share on other sites

[quote author=xyn link=topic=98993.msg389660#msg389660 date=1151675015]
hmm I'm slightly confused Now...

If i shoved the function in the top of my page then I just put [code=php:0]$String = filterWords($String);[/code] before the e-mail is actually submitted?
[/quote]

that's about it  8)
Link to comment
Share on other sites

Just to double check, Would this be correct?

[code=php:0]include("dir/db.php");
$db = mysql_connect("localhost", $login, $pwd); //I dont need the or die();
mysql_select_db("cpanel_dpanel"); //I don't need the or die();

function filterWords($String) {
  $sql = mysql_query("SELECT * FROM filter");
  $words = array();
  $replace = array();
  while ($x = mysql_fetch_array($sql)) {
    $words[] = $x['badword'];
    $replace[] = $x['replace'];
  }

  return str_replace($words, $replace, $String);
}

$msg="[b]BADWORD[/b], [b]BADWORD[/b] - Language filter testing";
$to="[b]EMAIL[/b]";
$sub="testing filter";

$String = filterWords($String);

mail( $to, $sub, $msg, "From: ash" );
echo 'check inbox';
exit;
[/code]
Link to comment
Share on other sites

[quote author=Barand link=topic=98993.msg389682#msg389682 date=1151677575]
As the variable you want to filter is $msq, better change that to

$msg = filterWords($msg);
[/quote]

Yepp That worked. Thank you!! =]
Link to comment
Share on other sites

And I have one final question

where it has [code=php:0]$msg = filterWords($msg);[/code]

can i use this to do multiple fields...
[code=php:0]$msg = filterWords($msg, $name, $mail);[/code]
Link to comment
Share on other sites

Change the function to this, note the arguments as now passed "by ref" using "&"

[code]<?php
function filterWords(&$w1, &$w2, &$w3) {
  $sql = mysql_query("SELECT * FROM filter");
  $words = array();
  $replace = array();
  while ($x = mysql_fetch_array($sql)) {
    $words[] = $x['badword'];
    $replace[] = $x['replace'];
  }

  $w1 = str_replace($words, $replace, $w1);
  $w2 = str_replace($words, $replace, $w2);
  $w3 = str_replace($words, $replace, $w3);
 
}?>
[/code]

and call with

filterWords($msg, $name, $mail);

Link to comment
Share on other sites

That didn't work for me. My code is:

[code=php:0] $mail = $_POST[email];
$name = $_POST[name];
$arti = $_POST[artist];
$song = $_POST[song];

include("dPanel/db.php");
$db = mysql_connect("localhost", $login, $pwd);
mysql_select_db("zroxxco_dpanel");

function filterWords(&$w1, &$w2, &$w3, &$w4)
{
  $sql = mysql_query("SELECT * FROM filter");
  $words = array();
  $replace = array();
  while ($x = mysql_fetch_array($sql))
  {
    $words[] = $x['badword'];
    $replace[] = $x['replace'];
  }

$w1 = str_replace($words, $replace, $w1);
$w2 = str_replace($words, $replace, $w2);
$w3 = str_replace($words, $replace, $w3);
$w4 = str_replace($words, $replace, $w4);

}

filterWords($name, $mail, $arti, $song);[/code]
Link to comment
Share on other sites

[code=php:0] $mail = $_POST['email'];
$name = $_POST['name'];
$arti = $_POST['artist'];
$song = $_POST['song'];

include("dPanel/db.php");
$db = mysql_connect("localhost", $login, $pwd);
mysql_select_db("zroxxco_dpanel");

function filterWords($w1, $w2, $w3, $w4)
{
  $sql = mysql_query("SELECT * FROM filter");
  $words = array();
  $replace = array();
  while ($x = mysql_fetch_array($sql))
    {
      $words[] = $x['badword'];
      $replace[] = $x['replace'];
    }

$return['1'] = str_replace($words, $replace, $w1);
$return['2'] = str_replace($words, $replace, $w2);
$return['3'] = str_replace($words, $replace, $w3);
$return['4'] = str_replace($words, $replace, $w4);

            return $return;
}

$clean=filterWords($name, $mail, $arti, $song);
$name=$clean['1'];
$mail=$clean['2'];
$arti=$clean['3'];
$song=$clean['4'];[/code]

That's how I'd do it.
The only thing I dont understand is why you do this cleaning on the email... I mean, if someone calls himself this way, that's his problem =/

Orio.
Link to comment
Share on other sites

[quote author=Orio link=topic=98993.msg389750#msg389750 date=1151683602]The only thing I dont understand is why you do this cleaning on the email... I mean, if someone calls himself this way, that's his problem =/[/quote]

What do you mean? & that didn't work either :S.

I was trying to do a song requesting form, and trying to make sure there is no foul language used, I tried to check the fields before they where saved to the database but it doesn't seem to work :[
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.