0

我正在尝试从用户输入的评论中删除诅咒词。诅咒词是从文本文件中读取的,但问题是我读到的词与它应该是的词不匹配。明显的问题是 EOL 字符(显然),我使用 str_replace 替换了所有 EOL 字符,但它不影响结果。

这是我的代码:

while(!feof($myfile)){
    $array[$i]=fgets($myfile);
    $word=$array[$i];
    str_replace("\n","",$word,$count);
    echo $count;
    str_replace("\r","",$word,$cont);
    echo $cont;
    str_replace("\r\n","",$word,$con);
    echo $con;
    str_replace(" ","",$word,$co);
    echo $co;
    str_replace(PHP_EOL,"",$word,$c);
    echo $c;
    if($word==="anal")
    echo "afdsfdsa";
    $comment= str_replace($word,"****",$comment);

我从这里下载了诅咒词文本文件 我无法弄清楚问题是什么。为什么这两个词不匹配?

4

2 回答 2

0

要获得每个单词,为什么不尝试:

$file = file_get_contents($myfile);
$words = explode(PHP_EOL, $file);
于 2015-11-17T14:59:12.603 回答
-2

这是修改后的代码

$myfile = 'swearWords.txt';
$words=file_get_contents($myfile);
$array = explode(PHP_EOL,$words);
$comment = "f*** this s***";
$comment= str_replace($array,"Bleep",$comment);
echo $comment;

输出

​ Bleep this Bleep
于 2015-11-17T15:03:32.007 回答