How can I include break lines in mysql records?

放肆的年华 提交于 2019-12-23 06:15:01

问题


When someone types in the following to be put into a database:

Hello,

My name is John and I am 22, and looking for a good time.

My Phone Number is:
555-NOT-REAL

Sincerely,
John D.

I want the break lines to stay there, but it seems that when I pull the value back out it comes out as follows.

Hello, My name is John and I am 22, and looking for a good time. My Phone Number is: 555-NOT-REAL Sincerely,John D.

Is there a special kind of field that keeps the format of the text that is put in? I am pretty sure that it is already in there because when I look at the field in the database the breaklines do appear. Maybe it is the way I am retrieving it?

I prefer not to have to code something to put in HTML tags, haha. Thanks!


回答1:


If you are displaying the text in the browser, you have to put HTML tags in. Browsers do not automatically put linebreaks for you. This is most commonly done with the nl2br function:

nl2br($message);

However, you could also wrap your text around the <pre> tag, but I don't recommend it:

<pre>$message</pre>



回答2:


Read: nl2br




回答3:


<?php
$string= "this
is
a
string";

echo $string. "<br />";

echo nl2br($string);
?>

output:

this is a string
this
is
a
string



回答4:


If I recall correctly (it's been a while since I've played with MySQL), you should replace line breaks with the escape sequence \n



来源:https://stackoverflow.com/questions/791330/how-can-i-include-break-lines-in-mysql-records

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!