Added: 21st of December 2015
Updated On: 29th of November 2023
Viewed: 7,692 times
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.