DEV Community

Kota
Kota

Posted on

How to replace your \n to <br>

Have you ever try to replace '\n' to '<br>' by php

You should write this str_replace("\n", "<br>", $string);

If you write this str_replace('\n', "<br>", $string);

That will go bad because "\n" is actually 0x0A but '\n' is just 0x5C6E!

Top comments (4)

Collapse
 
moopet profile image
Ben Sinclair

PHP also has the nl2br function, because reasons.

Collapse
 
nkkota profile image
Kota

I am forgetting to write about nl2br
This method insert br tag in front of new line code
So if you don't want new line code live, You must make your original code.
But usually you can use nl2br

Collapse
 
molotovbliss profile image
B00MER

PHP_EOL constant will adept to the system being used on.

Collapse
 
petee profile image
Petee

Thank you for your answers, guys