What potential pitfalls should be considered when using nl2br() function in PHP to handle line breaks in text?
When using nl2br() function in PHP to handle line breaks in text, one potential pitfall to consider is that it may introduce additional line breaks if the input text already contains HTML line breaks (<br>). To avoid this issue, you can first use strip_tags() function to remove any existing HTML tags before applying nl2br() function.
$text = "<p>Hello, this is a text with <br> line breaks.</p>";
$clean_text = strip_tags($text);
$final_text = nl2br($clean_text);
echo $final_text;