tags:

views:

27

answers:

1

Users enter text through a text area element, much like we enter questions on StackOverflow.

What's the best way to preserve the line spacing in the database so that it can be reproduced on the front end? The idea is the text should appear with the same line breaks and blank lines as entered originally by the user.

+1  A: 

Well, HTTP POST data comes in (almost) just as the user typed it in the textarea; if your database stores what was typed verbatim, it isn't going to be a problem, really. I think what you are thinking of is how to display the information back so that it formats properly in a web browser.

For that, you'll have to modify the text when it comes out of the database, For instance, in PHP:

$formatted_text = preg_replace(
    # patterns
    array('/\\r?\\n/', '/ /'),

    # replacements
    array('<br />', '&nbsp;'),

    # input text
    $text_from_database
);

will preserve both line breaks and multiple spaces.

amphetamachine
thanks, it doesn't seem like mysql is storing the data verbatim ... how can i confirm this?
Crashalot
You could always try to submit some dummy information into the database and then pull it out again using the MySQL command-line interface.
amphetamachine