似乎 fgets 在它返回的所有内容之后放置了一个空格。 下面是一些示例代码:
<?php
Echo "Opening " . $_SERVER{'DOCUMENT_ROOT'} . "/file.txt" . "...<br>";
$FileHandle = @Fopen($_SERVER{'DOCUMENT_ROOT'} . "/file.txt", "r");
If ($FileHandle){
Echo "File opened:<br><br>";
While (!Feof($FileHandle)){
$Line = Fgets($FileHandle);
Echo $Line . "word<br>"; //Should be LINECONTENTSword, no space.
}
Fclose($FileHandle);
}
?>
返回
Opening /var/www/vhosts/cqe.me/httpdocs/file.txt...
File opened:
First line word
Second line word
3rd line word
Another line of text word
Blablablaword
为什么行的内容和“word”之间有一个空格? 为什么文件末尾没有这个空格 (Blablablaword)?
你能告诉我如何摆脱这个空间吗? 非常感谢:-)!
最佳答案
fgets()返回一行 和 换行符 (\n
)。然后,与所有其他空格一样,这将在 HTML 中呈现为空格。
要删除结尾的换行符,请使用
$line = rtrim($line, "\r\n");
// Trims \r, \n and \r\n, which are all considered
// newlines on different platforms
关于php - fgets结果后的自动空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3513182/