Surinder Singh
Surinder Singh

Reputation: 59

echo trims multiple white spaces in the middle of string

The following code outputs a b:

$var="a   b"; // I inserted 3 white spaces, and HTML is rendering only one
echo $var;

The issue is that if I store $var in a table, it will KEEP the white spaces, and while reading, it strips them back. The striped data gets as it is updated in another table, resulting in mismatch of same values in both the tables.

I tried google search and found in a thread that its how HTML behaves. Any hope on how can I fix this?

Upvotes: 4

Views: 1945

Answers (3)

cairnz
cairnz

Reputation: 3957

Multiple spaces are trimmed in the html output of most browsers, not in the html code itself. If you want multiple spaces to render you can use str_replace(" ", " ",$var) which will tell the browsers to render each whitespace character. The same goes for multiple linebreaks, using nl2br()

Upvotes: 3

Esailija
Esailija

Reputation: 140220

The normal white-space behavior is to collapse everything to one space. You can modify it by changing the white-space: css rule to white-space: pre; or white-space: pre-wrap;

See https://developer.mozilla.org/en/CSS/white-space for more details

Upvotes: 7

PeeHaa
PeeHaa

Reputation: 72672

HTML always strips extra whitespace characters (for displaying). If you want to prevent this use  . However it sounds like you are using it for alignment. Never do that!

What if the text changes?

Upvotes: 0

Related Questions