Html 如何将用户输入的换行符从 TextArea 保存到数据库?

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

How to save user-entered line breaks from a TextArea to a database?

html

提问by Hakan F?st?k

How do I save user-entered line breaks from a <textarea>HTML element to a database?
It always removes the line breaks.

如何将用户输入的换行符从<textarea>HTML 元素保存到数据库?
它总是删除换行符。

回答by Hakan F?st?k

Pure CSS solution, NOT php specific
In normal cases, the TextAreaHTML element is preserving the white space in the database.
The problem appears when trying to display the \non the web browser, which will fail.

纯 CSS 解决方案,而不是特定
php在正常情况下,TextAreaHTML 元素会保留数据库中的空白。
尝试\n在 Web 浏览器上显示 时会出现此问题,这将失败。

To display \nin the web browser use :

\n在 Web 浏览器中显示,请使用:

<p style="white-space: pre-line">multi-line text</p>

回答by Jeremy Ruten

When displaying the content you need to convert line breaks into <br />tags, otherwise the web browser won't display them. This is probably why you think they aren't being saved. If you're using PHP, use the nl2br()function to do this. In other languages you could do a string replace, replacing all occurrences of "\n"with "<br />".

显示内容时需要将换行符转换为<br />标签,否则浏览器将不显示。这可能就是你认为他们没有得救的原因。如果您使用的是 PHP,请使用nl2br()函数来执行此操作。在其他语言中,你可以做一个字符串替换,替换所有出现的"\n""<br />"

回答by Lode Alaert

Just put the output text between <pre></pre>tags, that will preserve the line breaks.

只需将输出文本放在<pre></pre>标签之间,这将保留换行符。

回答by John Kariuki

I just learnt to use php's nl2brfunction and it is working fine for me. I'm using it to style how my users receive an email when sent from another user.

我刚刚学会了使用 php 的nl2br函数,它对我来说很好用。我正在使用它来设计我的用户如何接收来自其他用户的电子邮件。

回答by Joe Soul-bringer

I know from experience that Browser text areas are less well-behaved than one would like, especially with regard to line breaks.

我从经验中知道浏览器文本区域的表现不如人们想要的好,尤其是在换行符方面。

You could can to see if javascript would be able to interrogate the text area and find the line breaks before the text is sent to the server and so send the data in a more well-formatted way. But the amount of javascript debugging necessary to make this work across multiple browsers is probably not worth the effort.

您可以查看 javascript 是否能够在将文本发送到服务器之前询问文本区域并找到换行符,从而以更格式良好的方式发送数据。但是在多个浏览器上进行这项工作所需的 javascript 调试量可能不值得付出努力。

Perhaps you should say that format you are trying to capture your data. There may be a better way to get the data than keeping track of line-breaks - though lines breaks can seem like any easy thing to capture in user input.

也许您应该说您正在尝试捕获数据的格式。可能有比跟踪换行符更好的获取数据的方法 - 尽管换行符似乎很容易在用户输入中捕获。

回答by mogilka

I noticed that breakable content saved normally if textarea is inside a html form. But if I use textarea without any form and try to edit a long text in it, insert line breaks and save content via ajax, it's saved as a merged text into database

我注意到如果 textarea 在 html 表单中,则可正常保存的易碎内容。但是如果我使用没有任何形式的 textarea 并尝试在其中编辑长文本,插入换行符并通过 ajax 保存内容,它会作为合并文本保存到数据库中