views:

43

answers:

2

I am using <pre> tags to preserve long strings of text that include user-entered line breaks.

When the input into the original form was exactly:

Paragraph 1

Paragraph 2

Paragraph 3

...the pre returns:

            Paragraph 1

     Paragraph 2

     Paragraph 3

In the database, the string is stored:

"Paragraph 1\r\n\r\nParagraph 2\r\n\r\nParagraph 3."

  1. Why is this happening?
  2. If the best way to get around this to simply strip out all leading spaces from every newline, how can I accomplish this so it happens across the entire site?

Edit: I'm using Ruby on Rails - what would be the code in that case? A particular problem I'm having is with a HAML page.

%pre
  - unless condition.blank?
    %br
    - unless @user.show_notes == false
      :preserve
      #{@user.notes}
+3  A: 

You're probably indenting your code, eg (PHP example)

<pre>
    <?php echo $myString ?>
</pre>

Notice the space before the opening <?php tag?

Phil Brown
Hi Phil, thank you! That was indeed the problem with my html pages - I'm now trying to figure out the remaining examples in haml (see above).
sscirrus
A: 

I don't know if using <pre> to preserve user entered line breaks is best.

If you are using PHP, just use nl2br().

Then you don't have to worry about indentation in your HTML...

alex