我如何打印空白字符串在PHP 5.4


How do I print blank strings in PHP 5.4?

我尝试在PHP中使用"或''空字符串,但当使用echo语句在浏览器中显示它时,它似乎已被修剪。

echo '   the quick brown fox jumps over the lazy     dog.';

上面的语句被去掉了空白/空格。

如何在PHP中做到这一点?如何打印空行而不将其剥离?

PHP没有剥离任何内容;您的浏览器忽略了空格。右键单击,查看源代码,你就会明白我的意思了。要么将整个字符串包装在<pre>标记中,要么使用不间断的空格,例如,&nbsp;,用于您想要留下的每一块空白。(或者在元素上使用style='white-space: pre;')

echo '&nbsp;&nbsp; the quick brown fox jumps over the lazy &nbsp;&nbsp;&nbsp; dog.';

或:

echo '<pre>   the quick brown fox jumps over the lazy     dog.</pre>';

或:

echo '<p style="white-space: pre;">   the quick brown fox jumps over the lazy     dog.</p>';

或(@SecondRekudo):

或者发送一个Content-type: text/plain header

看起来像:

header('Content-type: text/plain');

需要在任何输出到浏览器之前调用(否则您将得到警告并且不会发送标题)。