line breaks on comments textareas
A user post a comment on a forum using a text开发者_如何学Goarea, he presses enter to format his text which 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?
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?
Use nl2br()
It will convert new lines to <br>
, so html will understand it.
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).
You mean this?:
$html_text = nl2br($text);
精彩评论