Html 在 textarea 中保留换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30593103/
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
Preserve line breaks in textarea
提问by Simon M.
I have a form with a textarea and I want to preserve line breaks entered by the user when outputting the content.
我有一个带有 textarea 的表单,我想在输出内容时保留用户输入的换行符。
For exemple, if I write in textarea :
例如,如果我在 textarea 中写:
Here is a sentence. Here is another. here is one more.
This is a new paragraph. Here is a new sentence. Here is another.
这里有一句话。这是另一个。这里还有一个。
这是一个新的段落。这里有一个新句子。这是另一个。
I want the same output and not:
我想要相同的输出而不是:
Here is a sentence. Here is another. here is one more. This is a new paragraph. Here is a new sentence. Here is another.
这里有一句话。这是另一个。还有一个。这是一个新的段落。这里有一个新句子。这是另一个。
How could I preserve line breaks?
我怎样才能保留换行符?
回答by Vaidotas Pocius
Generally you just need to add
通常你只需要添加
white-space: pre-line;
whitespace trimmed to single whitespace orwhite-space: pre-wrap;
all whitespace preserved
white-space: pre-line;
空白被修剪为单个空白或white-space: pre-wrap;
保留所有空白
to the element's style (CSS), where you want your text rendered with line-breaks.
到元素的样式 (CSS),您希望在其中使用换行符呈现文本。
回答by arkascha
The line breaks the user entered arepreserved, neither html nor php simply drops or alters anything. However html markup, when rendered for visualization uses a differentway to code line breaks. There are verygood reasons for that. So what you have to do is "translate" the existing line breaks into html style line breaks.
用户输入的换行符被保存下来,无论是HTML还是PHP直接丢弃或变造任何东西。然而 html 标记,在呈现为可视化时使用不同的方式来编码换行符。有非常好的理由。所以你要做的是将现有的换行符“翻译”成 html 样式的换行符。
How to do that depends on the environment you are working under. In general you have to translate line break codes like \n
to <br>
tags. The php
scripting language offers a function for this for example: nl2br()
如何做到这一点取决于您工作的环境。通常,您必须将换行符代码转换\n
为<br>
标签。该php
脚本语言提供了一个功能,这个例如:nl2br()
However take care: this only applies when renderingthe text as html markup. This does notapply, when you output the text into a textarea inside a form again to allow changing it for example. In that case you have to preserve the original line breaks just as received.
但是请注意:这仅适用于将文本呈现为 html 标记时。但这并不适用,当你的文本输出到一个文本形式的内部再次允许改变它的例子。在这种情况下,您必须保留原样的原始换行符。
So what you typically do is: you save the unaltered text input as received. When outputting that text again to a client, say after reading it from a database where you have saved it before, then you know the situation, how the text will be presented. That is when you either translate or leave the existing line breaks as they are.
所以你通常做的是:将未更改的文本输入保存为接收到的。当再次向客户端输出该文本时,比如从之前保存它的数据库中读取它之后,你就会知道这种情况,文本将如何呈现。那就是当您翻译或保留现有换行符时。
You couldalso encode unaltered text containing line breaks by <pre>...</pre>
tags, so mark them as to be rendered as preformatted. THis is for example done when displaying source code in html pages.
您还可以通过<pre>...</pre>
标签对包含换行符的未更改文本进行编码,因此将它们标记为按预格式化呈现。例如,这是在 html 页面中显示源代码时完成的。
回答by Eden Sharvit
You can do:
你可以做:
white-space: pre-wrap;
overflow-wrap: break-word;
回答by awadhesh
You keep output in textarea as it is. You would receive input as a string output that string(write to file)
that string adding textarea to the input string.
您将输出按原样保留在 textarea 中。您将接收作为字符串输出的输入,string(write to file)
该字符串将 textarea 添加到输入字符串。
for eg.
<?php
$txt = $_POST["inputstr"];
$txt1 = "<textarea>". $txt ."</textarea>";
$file = fopen("file.html","a+");
fwrite($file, $txt1);
fclose($file);
?>