Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

retaining text format, specifically whitespace's

Status
Not open for further replies.

shnazz

Technical User
Jul 27, 2004
22
US
Hi,

I have a couple of textbox's. When I store them into my mysql database they retain the text formatting. When I display them in a table on my site they lose the formatting. I used nl2br() to retain the line spacing.

Problem:
The spacing in between words is limited to one, and the spacing (if any) at the beginning of the textbox gets lost altogether. What can I do to retain the whitespace? I figure there is some kind of function like nl2br for such things but I cannot find one.

Also:
Is there maybe a better way of storing and pulling text from a mysql database so that all these character conversions do not need to happen in order to retain the text formatting?

Any help is appreciated,

- ShnaZZ
 
I have a couple of textbox's. When I store them "

What I meant to say is that when I store the text inputed into the textboxes...

just wanted to clear any confusion :)
 
The formatting could be getting lost in many places...

1) The browser could format the string before it posts it (unlikely)
2) The web server could strip the spaces before it gives it to php (unlikely)
3) PHP could strip some of the spaces, especially leading and trailing (possible)
4) You could be inavertantly doing something, perhaps a side effect of nl2br() (not impossible)
5) Mysql could be munging it (unlikely)
6) The browser could be ignoring the spaces because HTML doesn't care about it (probably).

Try examining the page source, or wrap the text in <pre> tags to see if #6 is the case. Otherwise backtrack and find where the problem is being introduced, then get back to us.
 
a whitespace is not the same as a new line, with nl2br() function all new lines are changed to <br>.

whitespaces are lost (or show only one in the browser, example:

text1: "My name is chacal"
text2: "My name is chacal"

if I put :
<?php
echo "<p>" $text1 "</p>;
echo "<p>" $text2 "</p>;
?>

both texts will be showed "My name is chacal", why? because browsers will erase all not meaning spaces. So if you want to preserve the spaces you can:

<pre> the text </pre>

or replace all whitespaces with "&nbsp;" (wiht no " char)

cheers.
 
adding the <pre> tag did the job. :)

Thanks eric and chacal.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top