简体   繁体   English

从带有换行符的文本区域到数据库的数据

[英]Data from textarea with line breaks to database

I have a simple textarea.我有一个简单的文本区域。 Once a save button is clicked, I save that data using an angular controller.单击保存按钮后,我将使用角度控制器保存该数据。 Then once an update button is clicked, I update a row in that data base, that one of that rows fields is the data from that textarea.然后单击更新按钮后,我会更新该数据库中的一行,其中一个行字段是来自该文本区域的数据。

So far so good, and the update is done ok, the problem begins when I break lines in the textarea, for example, I type:到目前为止一切顺利,更新完成,当我在文本区域中换行时,问题就开始了,例如,我输入:

"Hi,↵This is another row" “嗨,↵这是另一行”

I checked that data and when I save it in the controller, and when I get it back from the server it looks like this: "Hi,↵This is another row".我检查了该数据,当我将它保存在控制器中时,当我从服务器取回它时,它看起来像这样:“嗨,↵这是另一行”。

Seems ok, but when I place it inside a div I get the following:看起来不错,但是当我把它放在一个 div 中时,我得到以下信息:

"Hi,This is another row" “嗨,这是另一排”

Where did the line break go?断线去哪儿了? and why is it not displaying?为什么不显示?

I tried bunch of things, like changing the newline to <br> once send to the database, later on switching it back to newline, nothing seems to work.我尝试了很多东西,比如将换行符更改为<br>一旦发送到数据库,稍后将其切换回换行符,似乎没有任何效果。

Okay - what you need to do is use white-space:pre-wrap;好的 - 你需要做的是使用white-space:pre-wrap; where you display the data from the database.显示数据库中数据的位置。

Example:例子:

<?php
echo '<p style="white-space:pre-wrap;">'.$yourVarWithWhiteSpace.'</p>';
?>

Simply use this只需使用这个

<?php
echo '<p>'. nl2br($yourVarWithWhiteSpace).'</p>';
?>

You can programatically change the \n line ending with a <br /> tag on the way in or out.您可以通过编程方式更改以<br />标记结尾的 \n 行的进出方式。 By using prepared statements the need for using something like real_escape_string is handled for you.通过使用准备好的语句,可以为您处理使用诸如 real_escape_string 之类的东西的需要。

That said, using @itamar 's method is probably better in the long term.也就是说,从长远来看,使用@itamar 的方法可能更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM