PHP -如何创建换行字符?

时间:2021-11-04 21:44:54

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

John Doe\ n \n \ 1 John Doe\ n \ 1 John Doe\ n \

I have tried many variations of the \r\n, but none work. Why isn't the newline turning into a newline?

我试过很多变型的\r\n,但都没用。为什么换行线不变成换行线?

13 个解决方案

#1


519  

Only double quoted strings interpret 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 function chr(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函数chr(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),请确保检查编辑器的断行设置。

#2


179  

For platform independent line ending you can use the predefined PHP_EOL constant, as in:

对于平*立的行尾,可以使用预定义的PHP_EOL常数,如:

echo $clientid, ' ', $lastname, PHP_EOL;

#3


68  

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...

使用“echo”或使用fwrite…

fwrite([output text]."\n");

This will output HTML newline in place of "\n".

这将输出HTML换行符来代替“\n”。

#4


29  

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);
?>

#5


19  

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

#6


12  

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>One 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代码:
:

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";

#7


11  

You should use this:

你应该用这个:

"\n"

You also might wanna have a look at PHP EOL.

您可能还想看看PHP EOL。

#8


7  

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..

你可以用这个函数来做这件事。我试过其他方法,但他们不会帮助我。

#9


3  

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.

“Manu是一个好男孩。(输入)他能写好代码。”

echo nl2br($text);

回声nl2br($文本);

Result.

结果。

Manu is a good boy.

Manu是个好孩子。

He can code well.

他可以很好的代码。

#10


3  

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字符即可。

#11


2  

Use chr (13) for carriage return and chr (10) for new line

使用chr(13)作为回车,chr(10)作为新线路

echo $clientid;
echo ' ';
echo $lastname;
echo ' ';
echo chr (13). chr (10);

#12


1  

I have also tried this combination within both the single quotes and double quotes. But none has worked. Instead of using \n better use <br/> in the double quotes. Like this..

我也在单引号和双引号中尝试过这种组合。但是没有效果。与其使用\n,不如在双引号中使用
。像这样的. .

$variable = "and";
echo "part 1 $variable part 2<br/>";
echo "part 1 ".$variable." part 2";

#13


-1  

For any echo statements, I always use <br> inside double quotes.

对于任何echo语句,我总是在双引号内使用

#1


519  

Only double quoted strings interpret 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 function chr(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函数chr(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),请确保检查编辑器的断行设置。

#2


179  

For platform independent line ending you can use the predefined PHP_EOL constant, as in:

对于平*立的行尾,可以使用预定义的PHP_EOL常数,如:

echo $clientid, ' ', $lastname, PHP_EOL;

#3


68  

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...

使用“echo”或使用fwrite…

fwrite([output text]."\n");

This will output HTML newline in place of "\n".

这将输出HTML换行符来代替“\n”。

#4


29  

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);
?>

#5


19  

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

#6


12  

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>One 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代码:
:

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";

#7


11  

You should use this:

你应该用这个:

"\n"

You also might wanna have a look at PHP EOL.

您可能还想看看PHP EOL。

#8


7  

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..

你可以用这个函数来做这件事。我试过其他方法,但他们不会帮助我。

#9


3  

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.

“Manu是一个好男孩。(输入)他能写好代码。”

echo nl2br($text);

回声nl2br($文本);

Result.

结果。

Manu is a good boy.

Manu是个好孩子。

He can code well.

他可以很好的代码。

#10


3  

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字符即可。

#11


2  

Use chr (13) for carriage return and chr (10) for new line

使用chr(13)作为回车,chr(10)作为新线路

echo $clientid;
echo ' ';
echo $lastname;
echo ' ';
echo chr (13). chr (10);

#12


1  

I have also tried this combination within both the single quotes and double quotes. But none has worked. Instead of using \n better use <br/> in the double quotes. Like this..

我也在单引号和双引号中尝试过这种组合。但是没有效果。与其使用\n,不如在双引号中使用
。像这样的. .

$variable = "and";
echo "part 1 $variable part 2<br/>";
echo "part 1 ".$variable." part 2";

#13


-1  

For any echo statements, I always use <br> inside double quotes.

对于任何echo语句,我总是在双引号内使用