无法删除PHP中的行尾字符

时间:2015-11-17 14:52:50

标签: php preg-match str-replace file-read end-of-line

我正在尝试从用户输入的注释中删除诅咒词。从文本文件中读取诅咒词, 但问题是我读的字不符合它的意思。 显而易见的问题是EOL Character(显然),我使用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);

I downloaded the curse word text file from here 我无法弄清问题是什么。为什么两个单词不匹配?

2 个答案:

答案 0 :(得分:0)

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

$file = file_get_contents($myfile);
$words = explode(PHP_EOL, $file);

答案 1 :(得分:-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