I have this code for saving line breaks in text area input to database:
$input = preg_replace("/(\n)+/m", '\n', $input);
On examining the input in the database, the line breaks are actually saved.
But the problem is when I want to echo it out, the line breaks do not appear
in the output, how do I preserve the line breaks in input and also echo them
out. I don’t want to use <pre></pre>.
You are replacing actual sequences of newlines in the
$inputwith the literal two character sequence\n(backslash + n) – not a newline. These need to be converted back to newlines when read from the database. Although I suspect you intend to keep these actual newlines in the database and should be using a double quoted string instead…Note that I have replaced the first string delimiters with single quotes and the second string with double quotes.
\nis a special sequence in a regular expression to indicate a newline (ASCII 10), but it is also a character escape in PHP to indicate the same – the two can sometimes conflict.