What is the best practice for saving and displaying text with new lines from a textarea in PHP?

When saving text with new lines from a textarea in PHP, it is important to properly handle the newline characters to ensure they are saved and displayed correctly. One common way to do this is by using the PHP function nl2br() to convert newline characters to HTML line breaks (<br>). This allows the text to be displayed with proper line breaks in HTML.

// Save text with new lines from a textarea
$text = $_POST[&#039;textarea_input&#039;];
$clean_text = nl2br(htmlspecialchars($text));

// Display the text with proper line breaks
echo $clean_text;