简体   繁体   中英

Banned words application

Here is my try to create banned words tool but in last step i'm getting error.

1 - Created simple form to input the words to be banned (ban.php)

<form name="form" method="post" action="add.php">
    <textarea name="bad" id="bad"></textarea>
    <input type="submit" name="submit" id="submit" size="12" value="submit">
</form>

2 - Now will post it through db (add.php)

//DB Connection
require_once("config.php");
$bad = $_POST['bad'];
$bad = mysql_real_escape_string($bad);
$bad = nl2br($bad);
$bad = explode("<br />",$bad);
foreach ($bad as $value)
{
    $sql = "update my_table set words='$value'";
    mysql_query($sql, $conn) or die(mysql_error());
}
echo "Done words added";

3- Now how to apply it ! check this where i can not do ! i wanna say when $test="shit"; then if $test == any of the words added then give banned message and if not then give passed message.

<?php
//DB Connection
require_once("config.php");
//example
$test = "shit";
$qry = "select * from my_table";
$result = mysql_query($qry) or die($qry);
$line = mysql_fetch_array($result);
$words = nl2br($line['words']);
$words = str_replace('<br />', '', trim($words));
if ($test == $words) 
{
    echo "bannd";
} 
else 
{
    echo "passed";
}

The problem is i do not know how to compare $test with every single word added since all are stored in db like this

INSERT INTO `my_table` VALUES (1, 'bad\r\nshit\r\ndrugs');

so any help ~thanks

get all the results in an array from the database for example.

$bannedWords = array('shit', 'bannedword2', 'banned word 3');

and then you can check it like this.

if(in_array($word, $bannedWord)) {
    //Banned word Found
} else {
    //No Banned word found
}

PS : you should re-consider to design your database table on how you will store the records of banned words. better of you store one record in one row. for example.

在此处输入图片说明

this way all it will be easier for you to maintain records.

Your database design breaks at least the first database normal form ( http://en.m.wikipedia.org/wiki/First_normal_form ). More specifically, it breaks atomicity, meaning hat your entries in a relation (table) are not atomic. It would be, if you'd, for example, had table entries:

  • drugs (id 1)
  • shit (id 2)
  • etc.

After that, comparison is straight-forward.

use str_replace() function

to replace array of banned words, with some text such as * * in any text that matches your banned words array

Maybe you should try storing words in separate table? This would make more sence, I guess. Doing so, you'll need to generate an "SELECT * FROM bad_words WHERE word = ..." query, or something alike. Otherwise, you just have to split the string you've got from the DB and compare your $test variable looping through the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM