", $string); does not work. The string displays in this format: bla...">

Preg_replace \ n in line

for some reason it is: preg_replace("/\\n/", "<br />", $string); does not work.

The string displays in this format: blah blah blah\nblah blah blah even after replacing preg.

All I want to do is change if for <br /> .

nl2br() does not work either, but as its plain text, I was not sure if this should be done.

thanks

** Update **

preg_replace works on a word in a line.: (

+7
source share
3 answers

try it

 str_replace("\n", "<br />", $string); 
+6
source

If you want to replace the literal \n rather than the actual newline, try:

 <?php echo preg_replace("/\\\\n/", "<br />", 'Hello\nWorld'); 

Pay attention to the number of backslashes. The double quote string /\\\\n/ interpreted by the PHP engine as /\\n/ . This string, passed to the preg engine, is interpreted as the letter \n .

Note that both PHP interpret "\n" as an ASCII character 0x0A . Similarly, the preg engine will interpret '/\n/' as a newline (not quite sure which one).

+7
source

Have you tried using the multiple line modifier on your RegEx?

 preg_replace("/\\n/m", "<br />", $string); 
+4
source

All Articles