How can the issue of line breaks not being replaced by <br> tags be addressed when using nl2br in PHP?

When using nl2br in PHP to convert newline characters to <br> tags, it may not always work as expected if the line breaks are represented by different characters. To address this issue, you can first normalize the line breaks to a consistent format (e.g., using str_replace) before applying nl2br.

$text = &quot;This is a text with\nline breaks\r\nin different formats.&quot;;
$text = str_replace(array(&quot;\r\n&quot;, &quot;\r&quot;), &quot;\n&quot;, $text);
echo nl2br($text);