Want to improve this question? Add details and clarify the problem by editing this post.
Closed 25 days ago.
The community reviewed whether to reopen this question 25 days ago and left it closed:
Original close reason(s) were not resolved
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
您是否尝试过使用 PHP EOL 常量?
--- 添加 ---
我刚刚意识到我的文件编辑器做了同样的事情,但不用担心,这只是编辑器放置在那里以表示有换行符的幽灵字符
你可以尝试这个
你可以尝试使用 php 解析文件内容以查看里面的内容
......棘手
Did you tried with PHP EOL constant?
--- 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
You could try to parse the file contents using php to see what's inside
...tricky
保罗的回答有正确的方法,但他有一个错误。
你需要的是:
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:
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
哦伙计们!只需使用
\r\n
代替 \n
Ohh Guys! Just Use
\r\n
insted of \n
您提供的代码中没有任何内容会生成这些空格,除非
$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 usetrim($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 number0
instead, useintval($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, useintval()
, 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.烦人的注册,你那里的东西绝对没问题。
PHP_EOL 和“\n”完全相同。
您提供的代码没有任何问题,因此它必须是 $commentnumber 的值,其末尾有一个空格。如上所述,通过 trim() 函数运行您的 $commentnumber 。
祝你好运。
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.
Good luck.
在阅读了您的代码和回复后,我提出了一个理论...
由于我看不出您的代码有什么问题,您是如何打开并读取该文件的?您实际上是在文本编辑器中打开它吗? 您是否使用 PHP 脚本来执行此操作?如果是,请使用文本编辑器打开该文件,并检查每行末尾是否确实有空格。如果确实有……那么,请忽略这个答案的其余部分。如果没有,请继续阅读。
例如,如果您使用这样的内容:
那么由于
file()
的工作方式,您总是会在行尾有一个空格。 确保每个$line
末尾没有空格 - 例如换行符。< br>由于 HTML 将所有空格(空格、制表符、换行符 等)处理为空格,因此如果
$line
末尾有空格,那么这些空格将在HTML 输出。解决方案:使用
rtrim($line)
删除行尾的空格。使用以下代码:不会出现与第一个示例相同的问题,并且行末尾的所有空格都会消失。
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:
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:wouldn't have the same problems as the first example, and all spaces at the end of the lines would be gone.
我刚刚找到了最好的解决方案!
就用这种方式=>
服务器或 PHP 区分 '\n' 和 "\n",double-cout 将被打印为实际空格或换行符
i just found the best solution!
just use this way =>
the server or PHP makes a difference between '\n' and "\n" the double-cout will be printed as an Actual space or new line
这是因为每次写入文件时,文件都会完成,
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