PHP关键字日志记录脚本中的Word阻止列表

时间:2011-06-26 09:49:58

标签: php mysql sql logging

我有一个PHP脚本,可以将搜索脚本上的查询记录到MySQL数据库中。但是,如果为不适当的术语输入查询,我不希望将其添加到数据库中。我想要一个我认为不合适的单词列表,在将该术语添加到数据库之前进行检查。我怎么能这样做呢?

我目前的PHP代码是:

<?php

$query=$_GET['q'];

logQuery($query);
function logQuery($query){
$query="insert into queries (query) values ('$query') on duplicate key update value=value+1";
mysql_query($query);
}

?>

2 个答案:

答案 0 :(得分:1)

$inappropriate = array('clod', 'hipster');
if (!in_array($word, $inappropriate)) {
    // Add word to database
}

答案 1 :(得分:0)

您只需将错误单词列表准备到数组中,然后使用以下代码将这些单词替换为 *

$badWords = array("badWord1", "badWord2", "badWord3", "badWord4", "badWord1");
$cleanUp = str_replace($badWords, "****", $originalString);

如果您想识别包括其扩展名在内的此类词语(例如,ing,ed,s,ly等),并回复一条说明不合适的消息,您可以执行以下操作。任何你应该如何获得这些坏词的完整列表来识别它。

// Array of Bad words
$words = array('badWord1','badWord2','badWord3','badWord14');
// Array of extention to words
$exten = array('','ed','ing','s');
// Input string
$str = 'this is a dam sentence';
// Create an array from input
$string = explode(' ',strtolower($str));
// Create a new array for all combinations of swear words
$wordList = array();
// Add all combinations to the new array
foreach($words as $word){
   foreach($exten as $ext){
      $wordList[] = $word.$ext;
   }
}
// Loop through each input word, and check if it is a bad word or not
// FALSE = Good Words
// TRUE = Bad Words
$badWord = FALSE;
foreach($string as $s){
   if(in_array($s, $wordList)){
      $badWord = TRUE;
   }
}
// Do something if output is good or bad in this case display a message
if($badWord)
   echo 'This input contains inappropriate content';
else
   echo 'This is valid input!';