How to Replace \n with br in PHP?

To replace \n (newline) characters with <br> tags in PHP, you can use the str_replace() function. Here’s an example:

<?php
$inputText = "Hello\nThis is a multi-line\ntext.";

// Replace \n with <br>
$formattedText = str_replace("\n", "<br>", $inputText);

echo $formattedText;
?>

Output:

Hello<br>This is a multi-line<br>text.

In this example, we use the str_replace() function to find all occurrences of \n in the input string ($inputText) and replace them with <br>. The result is stored in the variable $formattedText, which is then echoed to display the text with <br> tags for newlines.