Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 895
Surinder Singh Avatar asked Dec 26 '22 22:12

Surinder Singh


2 Answers

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

like image 175
Esailija Avatar answered Dec 29 '22 12:12

Esailija


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()

like image 28
cairnz Avatar answered Dec 29 '22 12:12

cairnz