KP Joy KP Joy - 5 months ago 14
PHP Question

How to retain formatting of the content in PHP and MYSQL?

I have following text area:

<textarea name="message" rows="10" cols="40"></textarea>


I type any message in this
textarea
like this:

Hi,

Welcome to the new world.

From,
Admin


I save it to database.
Now, I fetch this field from the databases in a variable say
$msg
and I echo it like this:

echo $msg;


Everything is right. The message is displayed like this:

Hi, Welcome to the new world. From, Admin


All the content in one line. I want to display it like how it was typed in the
textarea
. Is there any way to do this?

Answer

Try to change:

echo $msg;

to

echo nl2br($msg);
Comments