What are the potential pitfalls of using nl2br() function in PHP?

The potential pitfall of using the nl2br() function in PHP is that it can lead to HTML injection vulnerabilities if not properly sanitized. To prevent this, it is important to use htmlspecialchars() function in conjunction with nl2br() to escape any HTML tags that may be included in the input.

$text = "<script>alert('Hello');</script>\nNew line";
$safe_text = nl2br(htmlspecialchars($text));
echo $safe_text;