PHP 换行符 (\n) 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3016034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:25:24  来源:igfitidea点击:

PHP Linefeeds (\n) Not Working

phpnewline

提问by ggutenberg

For some reason I can't use \nto create a linefeed when outputting to a file with PHP. It just writes "\n" to the file. I've tried using "\\n" as well, where it just writes "\n" (as expected). But I can't for the life of me figure out why adding \n to my strings isn't creating new lines. I've also tried \r\nbut it just appends "\r\n" to the line in the file.

出于某种原因,\n当使用 PHP 输出到文件时,我不能用来创建换行符。它只是将“ \n”写入文件。我也试过使用“ \\n”,它只写“\n”(正如预期的那样)。但是我终生无法弄清楚为什么将 \n 添加到我的字符串中不会创建新行。我也试过,\r\n但它只是将“ \r\n”附加到文件中的行。

Example:

例子:

error_log('test\n', 3, 'error.log');
error_log('test2\n', 3, 'error.log');

Outputs:

输出:

test\ntest2\n

Using MAMP on OSX in case that matters (some sort of PHP config thing maybe?).

在 OSX 上使用 MAMP 以防万一(可能是某种 PHP 配置?)。

Any suggestions?

有什么建议?

回答by ceejayoz

Use double quotes. "test\n"will work just fine (Or, use 'test' . PHP_EOL).

使用双引号。"test\n"会工作得很好(或者,使用'test' . PHP_EOL)。

If the string is enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters:

如果字符串用双引号 (") 括起来,PHP 将为特殊字符解释更多的转义序列:

http://php.net/manual/en/language.types.string.php

http://php.net/manual/en/language.types.string.php

回答by Huraiby

\nis not meant to be seen as a new line by the end user, you must use the html <br/>element for that. /n only affects how the html that is generated by php appears in the source code of the web page. if you go to your web page and click on 'view source' you will see php-generated html as one long line. Not pretty. That's what \nis for ;to break that php-generated html into shorter lines. The purpose of \nis to make a prettier 'view source' page.

\n并不意味着最终用户将其视为新行,您必须为此使用 html<br/>元素。/n 仅影响由 php 生成的 html 在网页源代码中的显示方式。如果您转到您的网页并单击“查看源代码”,您将看到 php 生成的 html 作为一行。不漂亮。这\n就是为了; 将 php 生成的 html 分解成更短的行。目的\n是制作一个更漂亮的“查看源代码”页面。

回答by nilesh

When you run a PHP script in a browser, it will be rendered as HTML by default. If the books you're using show otherwise, then either the code or the illustration is inaccurate. You can use “view source” to view what was sent to the browser and you'll see that your line feeds are present.

当您在浏览器中运行 PHP 脚本时,默认情况下它将呈现为 HTML。如果您使用的书籍另有显示,则说明代码或插图不准确。您可以使用“查看源代码”来查看发送到浏览器的内容,您会看到您的换行符存在。

<?php
echo "Line 1\nLine 2";
?>

This will render in your browser as:

这将在您的浏览器中呈现为:

Line 1 Line 2

If you need to send plain text to your browser, you can use something like:

如果您需要向浏览器发送纯文本,您可以使用以下内容:

<?php
header('Content-type: text/plain');
echo "Line 1\nLine 2";
?>

This will output:

这将输出:

Line 1
Line 2

回答by Souhaieb Besbes

I'm pretty sure you are outputting to a html file. The problem is html ignores newlines in source which means you have to replace the newlines with <br/>if you want a newline in the resulting page display.

我很确定您正在输出到 html 文件。问题是 html 忽略源代码中的换行符,这意味着<br/>如果您希望在结果页面显示中出现换行符,则必须替换换行符。

回答by Pankaj Monga

nl2br()function use for create new line

nl2br()用于创建新行的函数

echo nl2br("Welcome\r\n This is my HTML document", false);

The above example will output:

上面的例子将输出:

Welcome
This is my HTML document

欢迎
这是我的 HTML 文档

回答by oshirowanen

You need to use double quotes. Double quotes have more escape chars.

您需要使用双引号。双引号有更多的转义字符。

error_log("test\n", 3, 'error.log');
error_log("test2\n", 3, 'error.log');

回答by Matt Stephenson

Double quotes are what you want. Single quotes ignore the \escape. Double quotes will also evaluate variable expressions for you.

双引号是你想要的。单引号忽略\转义。双引号还将为您评估变量表达式。

Check this pagein the php manual for more.

查看php 手册中的此页面了解更多信息。

回答by Jan Kuboschek

It's because you use apostrophes ('). Use quotationmarks (") instead. ' prompts PHP to use whatever is in between the apostrophes literally.

这是因为您使用了撇号 (')。使用引号 (") 代替。 ' 提示 PHP 直接使用撇号之间的任何内容。

回答by aaron

to place the \nin double quotes try

放在\n双引号中试试



$LOG = str_replace('\n', "\n", $LOG);

回答by Ewer Ling

If you want to print something like this with a newline (\n) after it:

如果您想在其后使用换行符 (\n) 打印类似内容:

<p id = "theyateme">Did it get eaten?</p>

To print the above, you should do this:

要打印上述内容,您应该这样做:

<?php
  print('<p id = "theyateme">Did it get eaten?</p>' . "\n");
?>

The client code from above would be:

上面的客户端代码是:

<p id = "theyateme">Did it get eaten?</p>

The output from above would be:

上面的输出将是:

Did it get eaten?

被吃掉了吗?

I know it's hard, but I alwaysdo it that way, and youalmostalways have to do it that way.

我知道这很难,但我总是那样做,而几乎总是必须那样做。

Sometimes you want PHP to print \n to the page instead of giving a newline, like in JavaScript code (generated by PHP).

有时您希望 PHP 将 \n 打印到页面而不是提供换行符,就像在 JavaScript 代码(由 PHP 生成)中一样。

NOTE about answer: You might be like: Why did you use print instead of echo (I like my echo). That is because I prefer print over echo and printf, because it works better in some cases (my cases usually), but it can be done fine with echo in this case.

关于答案的注意事项:您可能会说:为什么使用打印而不是回声(我喜欢我的回声)。那是因为我更喜欢打印而不是 echo 和 printf,因为它在某些情况下(通常是我的情况)效果更好,但在这种情况下,使用 echo 可以做得很好。