Jump to content


Photo

Word Filter arrays...


  • Please log in to reply
27 replies to this topic

#1 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 30 June 2006 - 12:43 PM

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.

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 30 June 2006 - 12:53 PM

$badList = array('bad1', 'bad2', 'bad3');

$text = 'I hate this bad1 tiny code when you use bad2 code tags';

$clean = str_replace ($badList, '', $text);

echo $clean;

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 30 June 2006 - 12:56 PM

//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);};
};
 

Orio.
Think you're smarty?

(Gone until 20 to November)

#4 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 30 June 2006 - 01:05 PM

Thanks for the responces.

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

#5 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 30 June 2006 - 01:11 PM

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

 
//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);};
};
 

Orio.
Think you're smarty?

(Gone until 20 to November)

#6 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 30 June 2006 - 01:15 PM

Thanks for the responces.

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


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:
<?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);
}
?>

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.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#7 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 30 June 2006 - 01:19 PM

That was very helpful thanks,

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

#8 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 30 June 2006 - 01:25 PM

That was very helpful thanks,

And I'm guessing where you add the words and replacements into the database it would look like

  $words = array(badword1, etc.);
  $replace = array(replaceword1, etc.);
correct?


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:
INSERT INTO wordsTable (badWord, replacement) VALUES ('$myWord', '$myReplacement');

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#9 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 30 June 2006 - 01:32 PM

That was very helpful thanks,

And I'm guessing where you add the words and replacements into the database it would look like

  $words = array(badword1, etc.);
  $replace = array(replaceword1, etc.);
correct?


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:
INSERT INTO wordsTable (badWord, replacement) VALUES ('$myWord', '$myReplacement');


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?
<?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);
}
?>


#10 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 30 June 2006 - 01:36 PM

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.
Think you're smarty?

(Gone until 20 to November)

#11 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 30 June 2006 - 01:37 PM

Also where do i put the following code?


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:
$String = filterWords($String);

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#12 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 30 June 2006 - 01:43 PM

hmm I'm slightly confused Now...

If i shoved the function in the top of my page then I just put
$String = filterWords($String);
before the e-mail is actually submitted?

#13 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 30 June 2006 - 01:53 PM

hmm I'm slightly confused Now...

If i shoved the function in the top of my page then I just put

$String = filterWords($String);
before the e-mail is actually submitted?


that's about it  8)
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#14 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 30 June 2006 - 01:53 PM

Exactly.

Orio.
Think you're smarty?

(Gone until 20 to November)

#15 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 30 June 2006 - 02:07 PM

Just to double check, Would this be correct?

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;


#16 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 30 June 2006 - 02:26 PM

As the variable you want to filter is $msq, better change that to

$msg = filterWords($msg);
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#17 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 30 June 2006 - 02:32 PM

As the variable you want to filter is $msq, better change that to

$msg = filterWords($msg);


Yepp That worked. Thank you!! =]

#18 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 30 June 2006 - 03:02 PM

And I have one final question

where it has
$msg = filterWords($msg);

can i use this to do multiple fields...
$msg = filterWords($msg, $name, $mail);


#19 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 30 June 2006 - 03:17 PM

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

<?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);
   
}?>

and call with

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


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#20 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 30 June 2006 - 04:00 PM

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

$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);





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users