How can developers ensure that all line breaks are properly displayed when using nl2br() in PHP?

When using nl2br() in PHP to convert newline characters to HTML line breaks, developers need to ensure that the line breaks are properly displayed in the output. One common issue is that some environments may use different newline characters, such as \r\n or \n, which can lead to inconsistent line breaks. To solve this, developers can use the PHP function str_replace() to replace all newline characters with the standard \n before applying nl2br().

$text = "This is a text with \r\n newline characters.";
$text = str_replace(array("\r\n", "\r"), "\n", $text);
echo nl2br($text);