For some reason, preg_replace("/\\n/", "<br />", $string);
isn't working.
The string outputs in this format: blah blah blah\nblah blah blah
even after the preg replace.
I want to change if for a <br />
.
nl2br()
doesn't work either, but as it's just text, I wasn't sure if it should.
The preg_replace
function works on a word in the string. :(
If you want to replace the literal \n
and not the actual new line, try:
<?php
echo preg_replace("/\\\\n/", "<br />", 'Hello\nWorld');
Notice the number of backslashes. The double-quote enclosed string /\\\\n/
is interpreted by the PHP engine as /\\n/
. This string when passed on to the preg engine is interpreted as the literal \n
.
Note that both PHP will interpret "\n"
as the ASCII character 0x0A
. Likewise, the preg engine will interpret '/\n/'
as a newline character (I am not exactly sure which one/s).
Try this:
str_replace("\n", "<br />", $string);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With