What is the function nl2br() in PHP and how can it be used to preserve line breaks in text input?
The nl2br() function in PHP is used to convert newlines (\n) in a string to HTML line breaks (<br>). This is particularly useful when you want to preserve line breaks in text input from a textarea form field when displaying it on a webpage. By using nl2br(), you can ensure that the line breaks entered by the user are maintained in the output without the need for additional formatting.
<?php
$text = "Hello\nWorld!";
echo nl2br($text);
?>