What are common functions or methods in PHP that can help preserve line breaks and formatting when outputting data from a database?

When outputting data from a database in PHP, line breaks and formatting can be lost due to HTML rendering. To preserve line breaks and formatting, you can use the nl2br() function in PHP. This function converts newlines (\n) in a string to HTML line breaks (<br>), allowing the text to be displayed with proper formatting in the browser.

&lt;?php
// Retrieve data from the database
$data = &quot;This is a text with\nline breaks&quot;;

// Preserve line breaks and formatting using nl2br()
echo nl2br($data);
?&gt;