使用PHP从json中获取新行

问题描述:

So, i have in my database a text for example :

blabla
bloblo
blibli

I can see newlines if i look my text in my database. This text is from a textarea.

I have an Ajax request for get this text from my database.

In my php script, i do : echo json_encode($mytext) and in my ajax success i have my text in the var data.

But when i try to display this text data on my page, i loose all new lines. All my text is in on single line.. Any ideas ?

所以,我在我的数据库中有一个文本例如: p>

  blabla 
bloblo 
blibli 
  code>  pre> 
 
 

如果我在数据库中查看文本,我可以看到换行符。 这个文本来自textarea。 p>

我有一个Ajax请求从我的数据库中获取此文本。 p>

在我的php脚本中,我这样做: echo json_encode($ mytext) code>,在我的ajax成功中,我的文本在var data code>中。 p>

但是当我尝试 在我的页面上显示这个文本数据,我松开了所有新行。 我的所有文字都在一行..任何想法? p> div>

It's because the newline characters ( ) aren't new lines in HTML. In HTML you have to add either paragraphs (<p>) or line breaks (<br>) tags to accomplish this, depending on your scenario. I imagine you need to break line.

You can convert new lines into <br> tags using nl2br.

Reference: http://de1.php.net/nl2br

User nl2br function to display the data same as you entered in textarea.