我正在尝试开发一个 PHP 应用程序,它接受用户的评论,然后匹配字符串以检查评论是正面还是负面。我在 negative.txt 文件中有负面词列表。如果单词列表中的单词匹配,那么我想要一个简单的整数计数器加 1。我尝试了一些链接并创建了 a 代码来检查注释是负面还是正面,但它只匹配最后一个单词文件的。这是我所做的代码。
<?php
function teststringforbadwords($comment)
{
$file="BadWords.txt";
$fopen = fopen($file, "r");
$fread = fread($fopen,filesize("$file"));
fclose($fopen);
$newline_ele = "\n";
$data_split = explode($newline_ele, $fread);
$new_tab = "\t";
$outoutArr = array();
//process uploaded file data and push in output array
foreach ($data_split as $string)
{
$row = explode($new_tab, $string);
if(isset($row['0']) && $row['0'] != ""){
$outoutArr[] = trim($row['0']," ");
}
}
//---------------------------------------------------------------
foreach($outoutArr as $word) {
if(stristr($comment,$word)){
return false;
}
}
return true;
}
if(isset($_REQUEST["submit"]))
{
$comments = $_REQUEST["comments"];
if (teststringforbadwords($comments))
{
echo 'string is clean';
}
else
{
echo 'string contains banned words';
}
}
?>
最佳答案
我在您的 $comments
和文件输入周围添加了 strtolower
函数。这样,如果有人拼写 STUPID
,而不是 stupid
,代码仍然会检测到错误的单词。
我还添加了 trim
来删除不必要的和破坏性的空白(例如换行符)。
最后,我改变了你检查单词的方式。我使用了 preg_match
来分割所有空格,因此我们只检查完整的单词,并且不会意外地禁止不正确的字符串。
<?php
function teststringforbadwords($comment)
{
$comment = strtolower($comment);
$file="BadWords.txt";
$fopen = fopen($file, "r");
$fread = strtolower(fread($fopen,filesize("$file")));
fclose($fopen);
$newline_ele = "\n";
$data_split = explode($newline_ele, $fread);
$new_tab = "\t";
$outoutArr = array();
//process uploaded file data and push in output array
foreach ($data_split as $bannedWord)
{
foreach (preg_split('/\s+/',$comment) as $commentWord) {
if (trim($bannedWord) === trim($commentWord)) {
return false;
}
}
}
return true;
}
关于PHP 代码创建否定词词典并搜索帖子是否包含否定词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42800739/