如何在 PHP 中添加额外的空格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2300142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add extra whitespace in PHP?
提问by pnut
I was wondering how can I add extra whitespace in php is it something like \splease help thanks.
我想知道如何在 php 中添加额外的空格,\s请帮助谢谢。
Is there a tutorial that list these kind of things thanks.
有没有列出这些东西的教程,谢谢。
回答by Sarfraz
Like this:
像这样:
str_repeat(' ', 5); // adds 5 spaces
回答by Oddthinking
PHP (typically) generates HTML output for a web-site.
PHP(通常)为网站生成 HTML 输出。
When displaying HTML, the browser (typically) collapses all whitespace in text into a single space. Sometimes, between tags, it even collapses whitespace to nothing.
显示 HTML 时,浏览器(通常)会将文本中的所有空格折叠为一个空格。有时,在标签之间,它甚至会将空白折叠为空。
In order to persuade the browser to display whitespace, you need to include special tags like or <br/>in your HTML to add non-breaking whitespace or new lines, respectively.
为了说服浏览器显示空格,您需要在 HTML 中包含像 或 之类的特殊标签<br/>,以分别添加不间断的空格或新行。
回答by George
To render more than one whitespace on most web browsers use instead of normal white spaces.
要在大多数 Web 浏览器上呈现多个空格,请使用 代替普通空格。
echo "<p>Hello punt"; // This will render as Hello Punt (with 4 white spaces)
echo "<p> Hello punt"; // This will render as Hello punt (with one space)
For showing data in raw format (with exact number of spaces and "enters") use HTML <pre>tag.
要以原始格式显示数据(具有确切数量的空格和“输入”),请使用 HTML<pre>标记。
echo "<pre>Hello punt</pre>"; //Will render exactly as written here (8 white spaces)
Or you can use some CSS to style current block, not to break text or strip spaces (dunno bout this one)
或者你可以使用一些 CSS 来设置当前块的样式,而不是破坏文本或去除空格(不知道这个)
Any way you do the output will be the same but the browser itself strips double white spaces and renders as one.
输出的任何方式都是相同的,但浏览器本身会去除双空格并呈现为一个。
回答by Bitsping Technologies Pvt Ltd
for adding space character you can use
添加空格字符,您可以使用
<?
echo "\x20\x20\x20";
?>
回答by Anjani Barnwal
use this one. it will provide 60 spaces. that is your second parameter.
用这个。它将提供60个空间。这是你的第二个参数。
echo str_repeat(" ", 60);
echo str_repeat(" ", 60);
回答by Sabbir Ahmed Sourove
回答by Yada
pre is your friend.
pre 是你的朋友。
<pre>
<?php // code goes here
?>
</pre>
Or you can "View Source" in your browser. (Ctrl+U for most browser.)
或者您可以在浏览器中“查看源代码”。(对于大多数浏览器,Ctrl+U。)
回答by ma?ek
source
来源
<?php
echo "<p>hello\n";
echo "world</p>";
echo "\n\n";
echo "<p>\n\tindented\n</p>\n";
echo "
<div>
easy formatting<br />
across multiple lines!
</div>
";
?>
output
输出
<p>hello
world</p>
<p>
indented
</p>
<div>
easy formatting<br />
across multiple lines!
</div>
回答by Matt
is this for display purposes? if so you really should consider separating your display form your logic and use style sheets for formatting. being server side php should really allow providing and accepting data. while you could surely use php to do what you are asking I am a very firm believer in keeping display and logic with as much separation as possible. with styles you can do all of your typesetting.
这是用于展示目的吗?如果是这样,您真的应该考虑将显示与逻辑分开并使用样式表进行格式化。作为服务器端 php 应该真正允许提供和接受数据。虽然您肯定可以使用 php 来完成您所要求的操作,但我坚信尽可能多地分离显示和逻辑。使用样式,您可以进行所有排版。
give output class wrappers and style accordingly.
相应地给出输出类包装器和样式。
回答by David Morrow
to make your code look better when viewing source
在查看源代码时使您的代码看起来更好
$variable = 'foo';
echo "this is my php variable $variable \n";
echo "this is another php echo here $variable\n";
your code when view source will look like, with nice line returns thanks to \n
查看源代码时的代码看起来像,由于 \n 返回了漂亮的行
this is my php variable foo
this is another php echo here foo

