Try Amazon Prime for 30 days

Buy best selling Linux books

<< View More Computer Tips

Added On: 21/12/15   |    Category: PHP5    |    Number of Views: 1759

Preserve line breaks in PHP. nl2br function   

If your website allows users to enter information via a textbox, chances are they going to enter new lines as they enter their text. The problem is when you output this information, the line breaks will not be preserved.

To solve this issue in PHP, we can the use the nl2br function. The nl2br() function inserts HTML line breaks (<br />) in front of each newline (n) in a string.

In the example below, we presume that the user has just entered some text via a textbox on your website.

To preserve the line breaks you would use the following php code below.

<?php
echo nl2br($_POST['textarea']);
?>

If you are outputting information via your database, you would use the following code.
<?php
echo nl2br($row['textarea']);
?>


Search

Services

The Linux Foundation

Best selling Unix / Linux books
Nvidia Graphics Cards
Save 15% off .CO.UK domains at 123-Reg
Save 15% off .COM domains at 123-Reg
cPanel Hosting Plans
Xilo Web Hosting Plans