PHP-while循环(!feof())不会输出/显示所有内容

我想阅读(回声)一切为.txt档。


这是我的代码:


$handle = @fopen("item_sets.txt", "r");


while (!feof($handle))

{

    $buffer = fgets($handle, 4096);

    $trimmed = trim($buffer);

    echo $trimmed;

}

这是我的“ item_sets.txt”:http ://pastebin.com/sxapZGuW


但这并不能回显所有内容(并根据显示的内容和是否回显多少字符来更改显示的内容)。var_dump()告诉我最后一个字符串永远不会完成打印。看起来像这样:


" string(45) ""[cu_well_tra。但是如果我放一个


echo "whateverthisisjustarandomstringwithseveralcharacters";,


我的最后一条输出行如下所示:


" string(45) ""[cu_well_traveled_ak47]weapon_ak47" "1"

" string(5) "}

"


基本上,我的代码没有打印/回显它应该显示的所有内容,或者至少不显示它。


提前致谢 :)


至尊宝的传说
浏览 613回答 2
2回答

摇曳的蔷薇

那是因为您对EOF的测试是在您最后一次读取之前进行的在阅读过程中尝试进行EOF测试<?php$line_count = 0;$handle = fopen("item_sets.txt", "r");if ($handle) {&nbsp; &nbsp; while (($buffer = fgets($handle, 4096)) !== false) {&nbsp; &nbsp; &nbsp; &nbsp; $trimmed = trim($buffer);&nbsp; &nbsp; &nbsp; &nbsp; echo $trimmed;&nbsp; &nbsp; &nbsp; &nbsp; $line_count++;&nbsp; &nbsp; }} else {&nbsp; &nbsp; echo 'Unexpected error opening file';}fclose($handle);echo PHP_EOL.PHP_EOL.PHP_EOL.'Lines read from file = ' . $line_count;?>我还删除了忽略错误的不良做法的@内幕fopen,而寻找并处理它们的更好的做法。我将您的数据复制到一个名为tst.txt的文件中,并运行了此确切代码<?php$handle = fopen('tst.txt', 'r');if ($handle) {&nbsp; &nbsp; while (($buffer = fgets($handle, 4096)) !== false) {&nbsp; &nbsp; &nbsp; &nbsp; $trimmed = trim($buffer);&nbsp; &nbsp; &nbsp; &nbsp; echo $trimmed;&nbsp; &nbsp; }} else {&nbsp; &nbsp; echo 'Unexpected error opening file';}fclose($handle);它生成了此输出(此处仅显示一小部分)"item_sets"{"set_community_3"{"name"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "#CSGO_set_community_3""set_description"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "#CSGO_set_community_3_desc""is_collection"最后的输出是[aa_fade_revolver]weapon_revolver"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "1"数据文件中的最后一个条目
打开App,查看更多内容
随时随地看视频慕课网APP