tags:

views:

30

answers:

4

A user post a comment on a forum using a textarea, he presses enter to format his text wich means line breaks, how can i convert those line breaks to html breaks when inserting its message on the database so the message is echoed with the line breaks formatting?

cheers

+2  A: 

There's a function called nl2br(). Use it when echoing data, not when inserting to database. You don't want to display those <br/> tags when the user opens their post in textarea for editing, do you?

Mchl
Ok thanks that was the part i missed.
tada
+1  A: 

Use nl2br()

It will convert new lines to <br>, so html will understand it.

Centurion
A: 

Use nl2br() function. It replaces all newlines within a string with html br tags.

For example (taken from php manpage):

<?php
echo nl2br("foo isn't\n bar");
?> 

Outputs:

foo isn't<br />
 bar

The best practice is to use it when outputting data and store the data in the original ofrmat (with standard newlines).

silvo
A: 

You mean this?:

 $html_text = nl2br($text);
bartolsthoorn