How can you preserve “enters” given by the user in the database and show them then to other users?
I store the question of the user and use the following functions to sanitize the user data, prepare it, and execute the SQL command, respectively.
pg_escape_string
pg_prepare
pg_execute
I use htmlentities with ENT_QUOTES to convert the data HTML.
This procedure removes all enters, apparently in the form \n, in the question.
I would like to have a similar question-system as in SO: to show only double enters to users as line breaks.
After you call
htmlentities(), callnl2br(). The web browser ignores plain newline characters so you need to convert them to<br />elements to make them show up.For example:
To only show double newlines and ignore single ones, you could instead use a more sophisticated string replacement function,
preg_replace:Here
'/\n\s*\n/'matches a newline, followed by any amount of whitespace, followed by another newline. It replaces any such substring with two<br />elements. Single newlines are ignored. It’s also nice because it’ll ignore extraneous spaces and tabs that are invisible, such as if a user typed this: