How can the str_replace function be used to replace line breaks with HTML line breaks in PHP?

To replace line breaks with HTML line breaks in PHP, you can use the str_replace function to replace "\n" with "<br>". This allows you to display text with line breaks in an HTML format.

$text = &quot;This is a text with\nline breaks.&quot;;
$html_text = str_replace(&quot;\n&quot;, &quot;&lt;br&gt;&quot;, $text);
echo $html_text;