...

View Full Version : Bad Word filter (Mysql)



ajloun
08-25-2009, 11:48 PM
Hello

Looking for Simple php Script Useing Mysql to store the bad words , and be able to add more direct from the script ..

I made my Home work Searching but None come up with mysql all store on txt file..

Phil Jackson
08-26-2009, 12:22 AM
Try here: http://www.codingforums.com/forumdisplay.php?f=41
Not tried them (as i would make my own) but the guys on here are good so i trust their work.

whizard
08-26-2009, 12:26 AM
I'd recommend: http://www.codingforums.com/showthread.php?t=173314

Dan

seco
08-26-2009, 12:27 AM
Make a DB


CREATE DATABASE `badword` ;

CREATE TABLE `badword`.`words` (
`bw_id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`word` VARCHAR( 255 ) NOT NULL ,
PRIMARY KEY ( `bw_id` )
);



Addding to it. add_badword.php


<?php
if (isset($_POST['submit'])){
// Make a MySQL Connection
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("badword") or die(mysql_error());

//words variable from form
$words = $_POST['words'];

//inset word
mysql_query("INSERT INTO words (word) VALUES('$words') ")
or die(mysql_error());

echo "$words Added";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Add badword</title>
</head>

<body>
<form action="" method="post" name="badword">
<input name="words" type="text" /><input name="submit" type="submit" value="Add Badword" />
</form>
</body>
</html>

ajloun
08-26-2009, 01:10 AM
Thank you All .

@ seco that exactly wht i'm looking for , but Missing One Thing now is the Censor Function .. the file suppose to filter the world .. Can you make me a Favor and Creat it ..

@Phil Jackson , whizard ...... I been there .. No mysql .. Seco Got it Right for me

seco
08-26-2009, 01:25 AM
what are you censoring...a page or a form?

ajloun
08-26-2009, 01:44 AM
Post Removed

Zangeel
08-26-2009, 02:08 AM
the one whizard linked you to (http://www.codingforums.com/showthread.php?t=173314) can easily be modified for mysql. However in it's simplest for assuming you have the mysql tables set up this would work too http://codingforums.com/showthread.php?t=171988

bazz
08-26-2009, 02:13 AM
Make a DB

you could amke your table like this


CREATE TABLE words
( word VARCHAR( 255 ) NOT NULL primary key
);



bazz

ajloun
08-26-2009, 03:24 AM
Could someone Put it all in One script . the function file , the mysql tabel .. the form .

well be more clear if someone could make simple message board with this cesor .

@Zangeel I like Doese Not Mean بهب

I love = بهب
I like = ارغب
there Big Different .. :)

Zangeel
08-26-2009, 05:52 AM
Laa young grasshopper, أحب is I love, بهب is simply a visual representation of PHP, transliterated humorously as BHB as the lack of the letter P in the Arabic language. :D

Anyway, Go in PHPMYADMIN and run this sql code (make sure to make a database thru cPanel, they have vid tutorials if you don't know how):



CREATE TABLE badword (ID MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY, swears VARCHAR(60), replace VARCHAR(60))


Now you have your database, to populate it you do something like this in phpmyadmin:



INSERT INTO badword (`swears`, `replace`) VALUES ('arse', '****');
INSERT INTO badword (`swears`, `replace`) VALUES ('shiz', '****');
INSERT INTO badword (`swears`, `replace`) VALUES ('bith', '****');


Now you have your database setup.

Now just take a function like I posted in the other post



function wordcensor($txt)
{

//REMEMBER to connect to mysql here

$q = mysql_query("SELECT `swears`, `replace` FROM `badword`");
while ($row = mysql_fetch_array($q))
{
$txt = str_ireplace($row['swears'], $row['replace'], $txt);
}

return $txt;
}


Now when you're outputting a users string, wrap the function around it like


wordcensor($usersstring);

That'll check it for swears.

Most other questions like if you're new to phpmyadmin, or php mysql coding can be googled but this is the core of it.

ajloun
08-26-2009, 06:56 AM
Ya Not Grasshopper!.. I Got it ... I do Know Phpmyadmin, Mysql and some Php .. But i just Start Learning .. and I have Edited a Module of Making a Comments .. All Done well , But didt know how to intergate Censor word filter to the Module .

it seem to do it Mysql way Takes Much of Server Resurce , butter Done with Pure Php ..

So the Quastion now , ? wht if we have a Html Form to send A Comment , No php at all Just Html .. But the process file lets call it post.php Has first the Function of checking the inpute comment if there any bad words from a list of file lets call it badword.txt ..

lets put this in action.

1- the Html Form ( as i said no any php only Html ) the file name add.html


<html>

<head>

<title>New Page 1</title>
</head>

<body>

<form method="POST" action="post.php">
<p>
<textarea rows="2" name="S1" cols="20"></textarea></p>
<p><input type="text" name="T1" size="20"><input type="submit" value="Submit" name="B1"></p>
</form>
<p>&nbsp;</p>

</body>

</html>

2- Make the badword.txt file Like
badword=goodword

where = Means the badword will be replaced with the word after the =

3- the Process file Post.php


//Some Censor bad word Function....
// Check if word found in badword.txt If found Replace badword with good word
// Check if comment Posted then Send Function


Now Can you Do me Something like that , There is Simple way to do all that in javascript .. but Java easy to crack..

Zangeel
08-26-2009, 07:19 AM
The easiest way is just making an array with the badwords and their replacements. My post in the snippit section works with additional options to make sure no ones trying to bypass it. So you can do the same thing like



<html>

<head>

<title>New Page 1</title>
</head>

<body>

<form method="POST" action="post.php">
<p>
<textarea rows="2" name="S1" cols="20"></textarea></p>
<p><input type="text" name="T1" size="20"><input type="submit" value="Submit" name="B1"></p>
</form>
<p>&nbsp;</p>

</body>

</html>


Then for post.php



//my censoring function, or any other simples ones using an array

if (isset($_POST))
{
echo SmartCensor($_POST['S1']);
//example if u used my function (links posted here)
}


It's really very simple. If you really want a txt file (which i find to be unnecessarily) there's many examples here. Check this out here: http://codingforums.com/showthread.php?t=173512

ajloun
08-26-2009, 08:13 AM
the Thing that i tried your Script and didt work .. this How the post.php Files look after adding you Code.... where Have i done wrong

Zangeel
08-26-2009, 08:56 AM
Any errors? Does the page show at all?

ajloun
08-26-2009, 09:07 AM
No Error Just thankl yuo for comment .. and the Bad word Shows Up Proudly ..

Still Like the idea with text File , much easer to Edit.. Help Me Out with this Code Zangeel .



function censor($content){
//If you want to use the censor, make a file called censorwords.txt , and put each word you want censored on a new line.
//Written by Garrett P. www.garrettsites.net
$words_list=@file('censorwords.txt');
$search=array('a','b','i','l','o','p','s');
$replace=array('(?:a|\@|\*)','(?:b|8|3|\*)','(?:i|1|l|\!|\*)','(?:l|1|i|\!|\*)','(?:o|0|\*)','(?:p|\ ?|\*)','(?:s|\$|\*)');
foreach($words_list as $badword){
$badword=rtrim($badword);

$len=strlen($badword);
if($len <= 0)
continue;
$rep='';
for($i=0; $i < $len; $i++){
$rep.='-';
}

// Check to see if this word has brackets around it. If it is bracked, censor it in a simple form.
if (substr($badword, 0, 1) == "[" && substr($badword, ($len - 1), 1) == "]") {
$content = preg_replace("@".substr($badword, 1, ($len - 2))."@i", substr($rep, 0, (strlen($rep) - 2)), $content);
continue;
}

$badwordpreg=preg_split('//', $badword, -1, PREG_SPLIT_NO_EMPTY);
$badwordpreg=str_replace($search, $replace, $badwordpreg);
$badword='';
for($i=0; $i < count($badwordpreg); $i++){
$badword.=$badwordpreg[$i];
if($i != (count($badwordpreg)-1)) $badword.='(.{0,5})';
}
$badword="/$badword/i";
$content=preg_replace($badword, $rep, $content);
}
return $content;
}



this the Censor File and with the txt file been Created .. wht next ?
How Can i use this with My Code above ..

Zangeel
08-26-2009, 09:26 AM
OK Let's get to the basics. Make a file called swears.txt



<?php
function Swear($string)
{

$words = @file('swears.txt');
$censors = array();


foreach ($words as $word)
{
$censors[] = substr($word, 0, 1) . str_repeat('*', strlen($word) - 1);

}

$string = str_ireplace($words, $censors, $string);
return $string;
}
?>


List the swears in the file, and use this function to output the censored swears.

Phil Jackson
08-26-2009, 09:53 AM
I think the actual "bad word filter" could do with a little more development. Any kid older than 6 can quite easy put in such as:

ssjdhsj hjsdh sjdh @ss sjdhdjfh sh!t. or s h i t (i apologise codingForums, example purposes only)

regular expressions should be in place and sitting down with a pen and paper.

ajloun
08-26-2009, 12:14 PM
Thx Guys .. didt get it Quit Done .. But learn Some Good stuff .. :)

ajloun
08-26-2009, 12:37 PM
Oh zingeel about the last code u Provided .. it seems work but for some reason the Replacment word Get lost .. i can see after click post comment . the preview of the comment has the Censored Word .. but for some reason when the comment finaly entered the database , Shows the Actual word



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum