fgetc() 替换第一个字符

fgetc() replaces first character

提问人:LuisF 提问时间:11/7/2023 更新时间:11/7/2023 访问量:41

问:

为什么第一个字符在第二个 While 中被替换?

删除串联后,它可以正常工作,但是当我尝试在文件中的字符之间获取任何字符时,就会发生这种情况。

法典

<?php
$stream = fopen('./text.txt', 'r');

while (!feof($stream)) {
    $char = fgetc($stream);
    echo $char;
};

fclose($stream);

echo PHP_EOL;

$stream = fopen('./text.txt', 'r');

while (!feof($stream)) {
    //percorre cada linha do stream
    $char = fgetc($stream);
    echo "$char.";
};

fclose($stream);

文本.txt

Something
Something else
Other thing

结果

Something
Something else
Other thing
..o.m.e.t.h.i.n.g.
.S.o.m.e.t.h.i.n.g. .e.l.s.e.
.O.t.h.e.r. .t.h.i.n.g..
php 文件 字符串 -串联 fgetc

评论

0赞 brombeer 11/7/2023
没有串联。您是否尝试过 fgetc 页面中的示例?
1赞 Ken Lee 11/7/2023
(1)你说的不是可重复的 --- 看这个 (2)如上所述,没有串联,而是变量插值
1赞 brombeer 11/7/2023
也无法重现,得到S.o.m.e.t.h.i.n.g. .S.o.m.e.t.h.i.n.g. .e.l.s.e. .O.t.h.e.r. .t.h.i.n.g..
1赞 Barmar 11/7/2023
为什么while(!feof($file))总是错的
3赞 Barmar 11/7/2023
我怀疑该文件以非打印字符开头,可能是 BOM。

答: 暂无答案