Is using nl2br a better alternative to str_replace for converting line breaks in PHP?

When dealing with line breaks in PHP, using nl2br is generally a better alternative to str_replace. nl2br specifically converts newline characters to HTML line breaks, making it easier to display text with proper formatting in HTML documents. This function is designed for this specific purpose and is more efficient and reliable than using str_replace for this task.

$text = "Hello\nWorld";
$formatted_text = nl2br($text);
echo $formatted_text;