Are there any potential issues with using the nl2br function in PHP for converting line breaks?

One potential issue with using the nl2br function in PHP is that it can lead to double line breaks if the input text already contains HTML line breaks. To solve this issue, you can first strip any existing HTML line breaks before applying the nl2br function.

// Input text with potential HTML line breaks
$text = "Hello<br>World";

// Remove existing HTML line breaks
$text = preg_replace('/<br\s*\/?>/i', "\n", $text);

// Convert line breaks to HTML line breaks
$text = nl2br($text);

// Output the converted text
echo $text;