0
我目前正在对我正在处理的网站实施注释部分。我想阻止人们留下愚蠢的评论。我有一张表格,其中包含我不想在评论中留下的字词。到目前为止,我使用array_intersect来比较它和表单。我首先使用爆炸将提交的表单从字符串更改为数组。如果找不到任何不良词,则将该评论插入到数据库中。如果找到了一个禁忌词,那么现在我只回应一些东西来测试。我的问题是,array_intersect似乎返回的东西,即使没有找到匹配。在下面的代码中,print_r($ result)= Array()Array()。这是为什么?array_intersect返回一些东西,即使没有匹配存在
<?php
if (isset($_POST['name']) && isset($_POST['commentaire'])) {
try {
$db = new PDO("mysql:host=$server;dbname=etvoila;charset=utf8", $username, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));}
catch (Exception $e)
{
die('Erreur : ' . $e->getMessage());
}
$name = htmlspecialchars($_POST['name']);
$comment = htmlspecialchars($_POST['commentaire']);
validateWords($name);
validateWords($comment);
if ($badWord = 1)
{
echo "Bad word found!";
} else {
$insert = $db->prepare("INSERT INTO comments (name, comment) VALUES (:name, :comment)");
$insert->execute(array(
'name' => $name,
'comment' => $comment
));
echo "Thank you";
}
}
function validateWords($sentence) {
$badWord = 0;
try {
$bdd = new PDO("mysql:host=$server;dbname=etvoila;charset=utf8", $username, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));}
catch (Exception $e)
{
die('Error : ' . $e->getMessage());
}
$sentence2 = trim($sentence, ".!?");
$sentence2 = strtolower($sentence2);
$sentence2 = explode(" ",$sentence2);
$select = $bdd->prepare('SELECT words FROM forbiddenWords');
$select->execute();
while ($forbiddenWords = $select->fetch()) {
$result = array_intersect($sentence2,$forbiddenWords);
if ($result) {
$badWord = 1;
}
}
}
?>
这似乎并没有工作。同样的事情发生。在你给我的代码之后,我添加了echo $ badWord,但没有显示出来。我试过这个: $ result = array_intersect($ sentence2,$ forbiddenWords); print_r($ result); 和它打印Array()50次(我的表格包含大约50个单词) –
嗯,我认为我们在这里做错了什么,让我更新我的代码 –
@MichaëlFournier尝试print_r(count($ result));并看看结果是什么,并且可以print_r($ forbiddenWords)和print_r($ sentence2)一次,这样我就可以看到你的数组格式 –