Forum Moderators: coopster

Message Too Old, No Replies

Preserving Whitespace

         

mealybar

12:12 am on Mar 25, 2006 (gmt 0)

10+ Year Member



Using a textarea input what is the correct way to preserve whitespace?

coopster

12:35 am on Mar 25, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Not sure what you mean by preserving whitespace? You mean if you read it in and display the information back out again?

mealybar

12:48 am on Mar 25, 2006 (gmt 0)

10+ Year Member



I've got a text area, passed back through validation in php and saved to a text file, which is then used in some content on my site. If a user uses some spaces to emphasise a point or tabs to make an indent or rough table its being reverted back to the html 1 space.

Sure I've seen it before just cant remember, could be php function or html tag?

barns101

9:58 am on Mar 25, 2006 (gmt 0)

10+ Year Member



HTML ignores multiple spaces (not sure about tabs but I doubt they would appear the same as in a word processor document either). You could replace multiple spaces with their HTML equivalent ( ) in order to preserve formatting.

Alternatively you can use the preformatted text <pre> tag to preserve space. You could then add a style to this to format the font etc...

coopster

5:54 pm on Mar 25, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



nl2br [php.net] will return a string with '<br />' inserted before all newlines, is that what you are looking for?

Vastio

6:55 pm on Mar 25, 2006 (gmt 0)

10+ Year Member



I've used the following for one of my pages:

$string = nl2br(str_replace(" ", " &nbsp;", $string));
echo "$string";

When it reads the text file, it will preserve line breaks and mulitple character spaces.

mealybar

9:47 pm on Mar 25, 2006 (gmt 0)

10+ Year Member



Thanks all. I think it was <pre> I was looking for. Would using the string replace be a better solution?

barns101

3:41 pm on Mar 26, 2006 (gmt 0)

10+ Year Member



It would achieve the same effect but be more server intensive so I would use <pre> if it gets the job done.