Additional formatting on a <pre> tag
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."
- Why is this happening?
- 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开发者_C百科 page.
%pre
- unless condition.blank?
%br
- unless @user.show_notes == false
:preserve
#{@user.notes}
You're probably indenting your code, eg (PHP example)
<pre>
<?php echo $myString ?>
</pre>
Notice the space before the opening <?php
tag?
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...
精彩评论