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
Converting an "enter" in \n in a text file
提问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 \n
operator.
确保在\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;
回答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
回答by Pranav Hosangadi
You can append a \r\n
to the end of the text in the textbox.
您可以将 a 附加\r\n
到文本框中的文本末尾。
$textToAppend=$textInTextbox . '\r\n'
The \r\n
is the line break character sequence in Windows.\r
means carriage return and \n
means newline
The ASCII code foe \r
is 0x13
and for \n
is 0x10
的\r\n
是Windows换行符字符序列。\r
表示回车,\n
表示换行 ASCII 码 foe \r
is0x13
和 for \n
is0x10