回波处理字符串中间的多个空格

时间:2021-06-01 21:41:17

The following code outputs a b:

以下代码输出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.

问题是,如果我将$var存储在一个表中,它将保留空白,并且在读取时,它将它们删除。条带数据在另一个表中更新时获取,导致两个表中的相同值不匹配。

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

我尝试了谷歌搜索,在一个线程中发现了HTML的行为。我怎么解决这个问题?

3 个解决方案

#1


7  

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;

通常的空白空间行为是将所有东西折叠到一个空间。您可以通过将空白:css规则更改为空白:pre来修改它;或空白:pre-wrap;

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

详情请参见https://developer.mozilla.org/en/CSS/white-space

#2


3  

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

大多数浏览器的html输出中都有多个空格,而不是html代码本身。如果您希望呈现多个空格,可以使用str_replace(“”、“&检验”、$var),它将告诉浏览器呈现每个空格字符。对于使用nl2br()的多个换行符也是如此

#3


0  

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!

HTML总是删除额外的空格字符(用于显示)。如果你想防止这种使用。但是听起来你是在使用它进行对齐。从来没有那样做!

What if the text changes?

如果文本改变了怎么办?

#1


7  

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;

通常的空白空间行为是将所有东西折叠到一个空间。您可以通过将空白:css规则更改为空白:pre来修改它;或空白:pre-wrap;

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

详情请参见https://developer.mozilla.org/en/CSS/white-space

#2


3  

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

大多数浏览器的html输出中都有多个空格,而不是html代码本身。如果您希望呈现多个空格,可以使用str_replace(“”、“&检验”、$var),它将告诉浏览器呈现每个空格字符。对于使用nl2br()的多个换行符也是如此

#3


0  

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!

HTML总是删除额外的空格字符(用于显示)。如果你想防止这种使用。但是听起来你是在使用它进行对齐。从来没有那样做!

What if the text changes?

如果文本改变了怎么办?