php 在文本文件中转换 \n 中的“输入”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7136070/
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-26 02:07:20  来源:igfitidea点击:

Converting an "enter" in \n in a text file

phpformstextinclude

提问by Callum

I have a program that adds the content of a text box into a text file which I include on another page, the problem is that the include doesn't automatically create a new line where it is in the text file.

我有一个程序将文本框的内容添加到我包含在另一个页面上的文本文件中,问题是包含不会自动在文本文件中创建一个新行。

How do i do this?

我该怎么做呢?

Thanks for the help in advance!

我在这里先向您的帮助表示感谢!

采纳答案by Dave Lasley

If the issue is with viewing the newlines in the actual text box, \r\n, \r, or \n should do it (depending on your OS). Alternatively, if you want to view said newlines on the web you would use echo nl2br( $txt )

如果问题在于在实际文本框中查看换行符,\r\n、\r 或 \n 应该这样做(取决于您的操作系统)。或者,如果您想在网络上查看所述换行符,您可以使用 echo nl2br( $txt )

回答by

not sure what your asking, but a common mistake is assume the line break in a texatarea will be formatted in html for you, it wont. but in php you can just use the function nl2br() to convert a line break to the html <br>

不确定您的要求,但一个常见的错误是假设 texatarea 中的换行符将为您格式化为 html,它不会。但在 php 中,您可以使用函数 nl2br() 将换行符转换为 html<br>

回答by Steve Robbins

Make sure you're using double quotes around your \noperator.

确保在\n操作员周围使用双引号。

$text = "first line of text";
$text .= "\n" . $newText;

Edit:

编辑:

Or save a new line to a variable:

或将新行保存到变量:

<?    
    $newline = '
';
    $text = "first line of text";
    $newText = 'second line';
    $text .= $newline . $newText;
    echo $text;

http://codepad.org/pplKTiCN

http://codepad.org/pplKTiCN

回答by braindamage

$text = strip_tags($text, '<br>');

or you can replace the br tag using

或者您可以使用替换 br 标签

$text = str_replace ('<br>', "\n", $text);

http://php.net/manual/en/function.str-replace.php

http://php.net/manual/en/function.str-replace.php

also check http://www.php.net/manual/en/function.nl2br.php

还要检查http://www.php.net/manual/en/function.nl2br.php

回答by Pranav Hosangadi

You can append a \r\nto the end of the text in the textbox.

您可以将 a 附加\r\n到文本框中的文本末尾。

$textToAppend=$textInTextbox . '\r\n'  

The \r\nis the line break character sequence in Windows.
\rmeans carriage return and \nmeans newline The ASCII code foe \ris 0x13and for \nis 0x10

\r\n是Windows换行符字符序列。
\r表示回车,\n表示换行 ASCII 码 foe \ris0x13和 for \nis0x10