How can PHP be used to handle line breaks within a textarea input from a database?

When retrieving a textarea input from a database in PHP, line breaks may be stored as '\n' or '\r\n'. To display these line breaks correctly in HTML, you can use the PHP nl2br() function to convert newline characters to HTML <br> tags. This will ensure that line breaks are properly rendered in the textarea.

&lt;?php
// Retrieve textarea input from the database
$textarea_input = &quot;This is a text\nwith line breaks&quot;;

// Convert newline characters to HTML &lt;br&gt; tags
$textarea_output = nl2br($textarea_input);

// Display the textarea input with line breaks
echo &quot;&lt;textarea&gt;$textarea_output&lt;/textarea&gt;&quot;;
?&gt;