PHP - replace all "\n" Occurrences in a string

Solution 1:

I think you should use nl2br($myString);

https://www.php.net/manual/en/function.nl2br.php

<?
   $string = "Ceci\r\nest\n\rune\nchaîne\r";
   echo nl2br($string);

  /* will output
  Ceci<br />
  est<br />
  une<br />
  chaîne<br /> //*/
?>

Solution 2:

Try this:

PHP:

<?php

$myString = 'Hello\nWorld';

echo '<p>'. implode('</p><p>', explode('\n', $myString)) .'</p>';

?>

Output:

<p>Hello</p><p>World</p>