PHP - 如何创建换行符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4238433/
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
PHP - how to create a newline character?
提问by davidjhp
In PHP I am trying to create a newline character:
在 PHP 中,我试图创建一个换行符:
echo $clientid;
echo ' ';
echo $lastname;
echo ' ';
echo '\r\n';
Afterwards I open the created file in Notepad and it writes the newline literally:
之后我在记事本中打开创建的文件,它从字面上写出换行符:
1 John Doe\r\n 1 John Doe\r\n 1 John Doe\r\n
1 个约翰·多伊\r\n 1 个约翰·多伊\r\n 1 个约翰·多伊\r\n
I have tried many variations of the \r\n
, but none work. Why isn't the newline turning into a newline?
我尝试了 的许多变体\r\n
,但都不起作用。为什么换行符没有变成换行符?
回答by Gumbo
Only double quoted stringsinterpret the escape sequences \r
and \n
as '0x0D' and '0x0A' respectively, so you want:
只有双引号中的字符串解释转义序列\r
,并\n
因此要为“0X0D”和“的0x0A”分别是:
"\r\n"
Single quoted strings, on the other hand, only know the escape sequences \\
and \'
.
另一方面,单引号字符串只知道转义序列\\
和\'
。
So unless you concatenate the single quoted string with a line break generated elsewhere (e. g., using double quoted string "\r\n"
or using chr
functionchr(0x0D).chr(0x0A)
), the only other way to have a line break within a single quoted string is to literally type it with your editor:
因此,除非您将单引号字符串与在其他地方生成的换行符连接在一起(例如,使用双引号字符串"\r\n"
或使用chr
functionchr(0x0D).chr(0x0A)
),否则在单引号字符串中使用换行符的唯一其他方法是使用编辑器逐字输入:
$s = 'some text before the line break
some text after';
Make sure to check your editor for its line break settings if you require some specific character sequence (\r\n
for example).
如果您需要某些特定的字符序列(\r\n
例如),请确保检查您的编辑器的换行符设置。
回答by nnevala
Use the predefined PHP_EOL
constant:
使用预定义PHP_EOL
常量:
echo $clientid, ' ', $lastname, PHP_EOL;
The constant value will be set according to the line endings of the operating system where PHP is executing. On Linux, it will be "\n"
; on Windows, it will be "\r\n"
.
常量值将根据 PHP 正在执行的操作系统的行尾设置。在 Linux 上,它将是"\n"
; 在 Windows 上,它将是"\r\n"
.
回答by davea0511
The "echo" command in PHP sends the output to the browser as raw html so even if in double quotes the browser will not parse it into two lines because a newline character in HTML means nothing. That's why you need to either use:
PHP 中的“echo”命令将输出作为原始 html 发送到浏览器,因此即使在双引号中,浏览器也不会将其解析为两行,因为 HTML 中的换行符没有任何意义。这就是为什么你需要使用:
echo [output text]."<br>";
when using "echo", or instead use fwrite...
使用“回声”时,或改为使用 fwrite ...
fwrite([output text]."\n");
This will output HTML newline in place of "\n".
这将输出 HTML 换行符代替“\n”。
回答by Salvi Pascual
Use the constant PHP_EOL to get the right character no matter the platform.
无论平台如何,都使用常量 PHP_EOL 来获得正确的字符。
http://us3.php.net/manual/en/reserved.constants.php
http://us3.php.net/manual/en/reserved.constants.php
A simple usage example:
一个简单的使用示例:
<?php
$data = 'First line' . PHP_EOL .
'Second line' . PHP_EOL .
'Third line';
file_put_contents("filename.txt", $data);
?>
回答by Kevin S. Miller
Strings between double quotes ""
interpolate, meaning they convert escaped characters to printable characters.
双引号之间的字符串""
插入,这意味着它们将转义字符转换为可打印字符。
Strings between single quotes ''
are literal, meaning they are treated exactly as the characters are typed in.
单引号之间的字符串''
是字面量,这意味着它们的处理方式与输入的字符完全相同。
You can have both on the same line:
您可以将两者放在同一行上:
echo '$clientid $lastname ' . "\r\n";
echo "$clientid $lastname \r\n";
outputs:
输出:
$clientid $lastname
1 John Doe
回答by Wouter Dorgelo
回答by Shoe
Actually \r\n
is for the html side of the output. With those chars you can just create a newline in the html code to make it more readable:
实际上\r\n
是用于输出的 html 端。使用这些字符,您可以在 html 代码中创建一个换行符以使其更具可读性:
echo "<html>First line \r\n Second line</html>";
will output:
将输出:
<html>First line
Second line</html>
that viewing the page will be:
查看页面将是:
First line Second line
If you really meant this you have just to fix the single quote with the "" quote:
如果你真的是这个意思,你只需要用 "" 引号修复单引号:
echo "\r\n";
Otherwise if you mean to split the text, in our sample 'First line' and 'Second line' you have to use the html code: <br />
:
否则,如果您要拆分文本,则在我们的示例“第一行”和“第二行”中,您必须使用 html 代码<br />
:
First line<br />Second line
that will output:
这将输出:
First line
Second line
Also it would be more readable if you replace the entire script with:
如果您将整个脚本替换为以下内容,它也会更具可读性:
echo "$clientid $lastname \r\n";
回答by amin gholami
w3school offered this way:
w3school 以这种方式提供:
echo nl2br("One line.\n Another line.");
by use of this function you can do it..i tried other ways that said above but they wont help me..
通过使用这个功能你可以做到..我尝试了上面说的其他方法,但他们不会帮助我..
回答by user2574384
For some reason, every single post asking about newline escapes in PHP fails to mention the case that simply inserting a newline into single-quoted strings will do exactly what you think:
出于某种原因,每一篇关于 PHP 中换行符转义的帖子都没有提到简单地将换行符插入单引号字符串中的情况会完全符合您的想法:
ex 1.
例 1。
echo 'foo\nbar';
Example 1 clearly does not print the desired result, however, while it is true you cannot escape a newline in single-quotes, you can have one:
示例 1 显然没有打印出所需的结果,但是,虽然您确实无法在单引号中转义换行符,但您可以使用:
ex 2.
例 2。
echo 'foo
bar';
Example 2 has exactly the desired behavior. Unfortunately the newline that is inserted is operating system dependent. This usually isn't a problem, as web browsers/servers will correctly interpret the newline whether it is \r, \r\n, or \n.
示例 2 恰好具有所需的行为。不幸的是,插入的换行符取决于操作系统。这通常不是问题,因为 Web 浏览器/服务器会正确解释换行符,无论它是 \r、\r\n 还是 \n。
Obviously this solution is not ideal if you plan to distribute the file through other means then a web browser and to multiple operating systems. In that case you should see one of the other answers.
显然,如果您计划通过其他方式分发文件,然后通过 Web 浏览器分发到多个操作系统,则此解决方案并不理想。在这种情况下,您应该会看到其他答案之一。
note: using a feature rich text editor you should be able to insert a newline as a binary character(s) that represents a newline on a different operating system than the one editing the file. If all else fails, simply using a hex editor to insert the binary ascii character would do.
注意:使用功能丰富的文本编辑器,您应该能够将换行符作为二进制字符插入,表示与编辑文件的操作系统不同的操作系统上的换行符。如果所有其他方法都失败了,只需使用十六进制编辑器插入二进制 ascii 字符即可。
回答by Manu R S
Use the PHP nl2br to get the newlines in a text string..
使用 PHP nl2br 获取文本字符串中的换行符..
$text = "Manu is a good boy.(Enter)He can code well.
echo nl2br($text);
Result.
Manu is a good boy.
He can code well.
$text = "Manu 是个好孩子。(回车)他会写代码。
回声 nl2br($text);
结果。
马努是个好孩子。
他可以很好地编码。