file_put_contents、file_append 和换行符

发布于 2024-09-14 23:58:34 字数 1703 浏览 12 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

总攻大人 2024-09-21 23:58:34

您是否尝试过使用 PHP EOL 常量?


file_put_contents($filename, $commentnumber . PHP_EOL, FILE_APPEND)

--- 添加 ---

我刚刚意识到我的文件编辑器做了同样的事情,但不用担心,这只是编辑器放置在那里以表示有换行符的幽灵字符
你可以尝试这个

A file with EOL after the last number looks like:
1_
2_
3_
EOF

but a file without that last character looks like

1_
2_
3
EOF

where _ means a space character

你可以尝试使用 php 解析文件内容以查看里面的内容


$lines = explode( PHP_EOL, file_get_contents($file));
foreach($lines as $line ) {
    var_dump($line);
}

......棘手

Did you tried with PHP EOL constant?


file_put_contents($filename, $commentnumber . PHP_EOL, FILE_APPEND)

--- Added ---

I just realize that my file editor does the same, but don't worrie, is just a ghost character that the editor places there to signal that there is a newline
You could try this

A file with EOL after the last number looks like:
1_
2_
3_
EOF

but a file without that last character looks like

1_
2_
3
EOF

where _ means a space character

You could try to parse the file contents using php to see what's inside


$lines = explode( PHP_EOL, file_get_contents($file));
foreach($lines as $line ) {
    var_dump($line);
}

...tricky

淡莣 2024-09-21 23:58:34

保罗的回答有正确的方法,但他有一个错误。
你需要的是:

file_put_contents($filename, trim($commentnumber).PHP_EOL, FILE_APPEND);

PHP_EOL 常量确保在 mac、windows 和 unix 系统上使用正确的行结尾
修剪函数删除字符串两侧的任何换行符或空格。
转换为整数将是一个巨大的错误,因为
1.你最终可能会得到零,特别是因为空格或特殊字符(无论它们来自哪里......)
2. id不一定是整数

pauls answer has the correct approach but he has a mistake.
what you need ist the following:

file_put_contents($filename, trim($commentnumber).PHP_EOL, FILE_APPEND);

the PHP_EOL constant makes sure to use the right line ending on mac, windows and unix systems
the trim function removes any newline or whitespace on both sides of the string.
converting to integer would be a huge mistake because
1. you might end up having zero, expecially because of white space or special characters (wherever they come from...)
2. ids dont necessarily need to be integers

花桑 2024-09-21 23:58:34

哦伙计们!只需使用

\r\n

代替 \n

Ohh Guys! Just Use

\r\n

insted of \n

水中月 2024-09-21 23:58:34

您提供的代码中没有任何内容会生成这些空格,除非 $commentnumber 已包含开头的空格。如果是这种情况,只需使用 trim($commentnumber) 即可。

您的代码中也没有任何内容可以解释文件底部的空行,除非 $commentnumber 可以是空字符串。如果是这种情况,并且您希望它输出数字 0,请使用 intval($commentnumber)

当然,您只需要这两者之一。如果您想保留类似字符串的内容,请使用trim();如果您总是想要整数,请使用 intval(),它已经自动修剪它。

也有可能您不小心在实际代码中编写了 " \n" 而不是 "\n",但在您在此处发布的代码中它是正确的。

There is nothing in the code you provided that would generate those spaces, unless $commentnumber already contains the space to begin with. If that is the case, simply use trim($commentnumber) instead.

There is also nothing in your code that would explain empty lines at the bottom of the file, unless $commentnumber can be an empty string. If that is the case, and you want it to output the number 0 instead, use intval($commentnumber).

Of course, you need only one of those two. If you want to preserve string-like content, use trim(); if you always want integers, use intval(), which already trims it automatically.

It is also possible that you accidentally wrote " \n" instead of "\n" in your actual code, but in the code you posted here it is correct.

旧伤慢歌 2024-09-21 23:58:34

烦人的注册,你那里的东西绝对没问题。
PHP_EOL 和“\n”完全相同。

您提供的代码没有任何问题,因此它必须是 $commentnumber 的值,其末尾有一个空格。如上所述,通过 trim() 函数运行您的 $commentnumber 。

file_put_contents($filename, trim($commentnumber . "\n"), FILE_APPEND);

祝你好运。

annoyingregistration, what you have there is absolutely fine.
PHP_EOL and "\n" are exactly the same.

The code you provided theres nothing wrong with it so it must be the value of $commentnumber that has a space at the end of it. as stated, run your $commentnumber through the trim() function.

file_put_contents($filename, trim($commentnumber . "\n"), FILE_APPEND);

Good luck.

时常饿 2024-09-21 23:58:34

在阅读了您的代码和回复后,我提出了一个理论...

由于我看不出您的代码有什么问题,您是如何打开并读取该文件的?您实际上是在文本编辑器中打开它吗? 您是否使用 PHP 脚本来执行此操作?如果是,请使用文本编辑器打开该文件,并检查每行末尾是否确实有空格。如果确实有……那么,请忽略这个答案的其余部分。如果没有,请继续阅读。

例如,如果您使用这样的内容:

<?php
$lines = file($filename);
if($lines) // Error reading
  die();
foreach($lines as $line)
  echo $line."<br />";

那么由于 file() 的工作方式,您总是会在行尾有一个空格。 确保每个 $line 末尾没有空格 - 例如换行符< br>
由于 HTML 将所有空格(空格、制表符、换行符 等)处理为空格,因此如果 $line 末尾有空格,那么这些空格将在HTML 输出。

解决方案:使用rtrim($line)删除行尾的空格。使用以下代码:

<?php
$lines = file($filename);
if($lines) // Error reading
  die();
foreach($lines as $line)
  echo rtrim($line)."<br />";

不会出现与第一个示例相同的问题,并且行末尾的所有空格都会消失。

After reading your code and responses, I have come up with a theory...

Since I can't see that there's anything wrong with your code, how did you open and read the file? Did you actually open it in a text editor? Did you use a PHP script to do it? If so, open the file with a text editor and check that there are actually spaces at the end of each line. If there is actually is...well, ignore the rest of this answer, then. If not, just read on.

For instance, if you use something like this:

<?php
$lines = file($filename);
if($lines) // Error reading
  die();
foreach($lines as $line)
  echo $line."<br />";

Then you would always a whitespace at the end of the line because of the way file() work. Make sure each $line does not have a whitespace - such as a newline character - at the end.
Since HTML handles all whitespaces - spaces, tabs, newlines etc. - as spaces, if there is a whitespace at the end of $line, then those would appear as spaces in the HTML output.

Solution: use rtrim($line) to remove whitespaces at the end of the lines. Using the following code:

<?php
$lines = file($filename);
if($lines) // Error reading
  die();
foreach($lines as $line)
  echo rtrim($line)."<br />";

wouldn't have the same problems as the first example, and all spaces at the end of the lines would be gone.

谈场末日恋爱 2024-09-21 23:58:34

我刚刚找到了最好的解决方案!

就用这种方式=>

$content = str_replace('\n', "\n", $content );
$content = str_replace('\t', "\t", $content );
$content = str_replace('\s', "\s", $content );

file_put_contents($file_name, $content);

服务器或 PHP 区分 '\n' 和 "\n",double-cout 将被打印为实际空格或换行符

i just found the best solution!

just use this way =>

$content = str_replace('\n', "\n", $content );
$content = str_replace('\t', "\t", $content );
$content = str_replace('\s', "\s", $content );

file_put_contents($file_name, $content);

the server or PHP makes a difference between '\n' and "\n" the double-cout will be printed as an Actual space or new line

宛菡 2024-09-21 23:58:34

这是因为每次写入文件时,文件都会完成,file_put_contents 在末尾插入一个额外的换行符

its because each time you write to the file, the file is being finished, file_put_contents inserts an extra line break at the end

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文