简体   繁体   中英

Banned words application using database table part 2

Objective : I'd like to store certain words inside database table as banned words to not to be used within posts.

I've asked how to apply banned words application using database Banned words application using database table and thanks for all answers.

But i've some changes would like to apply but 1st here is the new code

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

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

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

<?PHP
require_once("config.php"); // db conn
$bad = $_POST['bad'];
$bad = mysql_real_escape_string($bad);

$sql= "insert into my_table set bad='$bad'";
mysql_query($sql, $conn) or die(mysql_error());

echo "Done bad word added";
?>

3- Example (1) ~ thanks to Robjong for previous help ~

<?PHP
require_once("config.php"); // db conn

$test = "ugly"; // Example

$qry = "SELECT * FROM `my_table` WHERE bad='{$test}'"; // select by value
$result=mysql_query($qry);

if( mysql_num_rows( $result ) ){ // we have a row with the 'bad' word
echo "banned";
}else{
echo "passed";
}
?>

Now my question Example (2)

What if i want to say if any of the stored words in my_table is found within $test then give echo "banned";

Just like this

<?PHP
require_once("config.php"); // db conn

$test = "hello world this is sentence with ugly word"; // 'ugly' word found


if( any stored words in my_table found within $test ){ 
echo "banned";
}else{
echo "passed";
}
?>

so how to do example (2) ?! that would helps me a lot.

How do you implement a good profanity filter?

Answers the same sort of question. I would avoid using database and maybe make an include file with the values. Otherwise you could build the regular expression from all the database tables.

Another option is checking each word manually against the database or generating the query dynamically, searching for each individual word.

一个幼稚的解决方案是遍历$ test中的单词并检查每个单词,如示例1所示。

I think it would be faster to pull out all of the banned words from your table and do the processing in PHP - rather than do the processing in MySQL. For that you would first select out each of the strings from the banned words table and put them in an array.

Once you have done that you can then use array_intersect to see if any of the words are within the string eg

//ASSUMES AN ARRAY OF BANNED WORDS $banned_words AND A STRING TO CHECK $str_check

if(count(array_intersect(array_map('strtolower', explode(' ', $str_check)), $banned_words) > 0) {
    echo "banned";
}
else {
    echo "not banned";
}

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