What are the potential pitfalls of using nl2br() and how can they be avoided?
Using nl2br() can potentially introduce HTML injection vulnerabilities if the input is not properly sanitized. To avoid this, it is important to sanitize the input before using nl2br(). One way to do this is by using htmlspecialchars() function to encode special characters in the input.
$input = "<script>alert('XSS attack')</script>";
$sanitized_input = htmlspecialchars($input);
$output = nl2br($sanitized_input);
echo $output;