views:

194

answers:

4

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!

A: 

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

Jason Musgrove
+7  A: 

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>
Paolo Bergantino
Works great, I wish I knew about this earlier!!!!!
Chris B.
+2  A: 

Read: nl2br

jmucchiello
+1  A: 
<?php
$string= "this
is
a
string";

echo $string. "<br />";

echo nl2br($string);
?>

output:

this is a string
this
is
a
string