What potential pitfalls should be avoided when using nl2br() function in PHP?
When using the nl2br() function in PHP to convert newlines to HTML line breaks, it's important to be cautious of potential security vulnerabilities such as cross-site scripting (XSS) attacks. To avoid this, it's recommended to properly sanitize user input before passing it to nl2br(). This can be done using functions like htmlspecialchars() to escape special characters.
// Sanitize user input before using nl2br()
$userInput = "<script>alert('XSS attack!')</script>";
$sanitizedInput = htmlspecialchars($userInput, ENT_QUOTES);
echo nl2br($sanitizedInput);