无法删除 PHP 中的行尾字符
Unable to remove end of line characters in PHP
我正在尝试从 user.The 输入的评论中删除脏话是从文本文件中读取的,
但问题是我读到的词与它本来应该是的词不符。
明显的问题是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);
I downloaded the curse word text file from here
我不知道是什么问题。为什么这两个词不匹配?
要得到每个单词,为什么不尝试:
$file = file_get_contents($myfile);
$words = explode(PHP_EOL, $file);
修改后的代码
$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
我正在尝试从 user.The 输入的评论中删除脏话是从文本文件中读取的, 但问题是我读到的词与它本来应该是的词不符。 明显的问题是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);
I downloaded the curse word text file from here 我不知道是什么问题。为什么这两个词不匹配?
要得到每个单词,为什么不尝试:
$file = file_get_contents($myfile);
$words = explode(PHP_EOL, $file);
修改后的代码
$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