How to add extra whitespace in PHP?

2019-01-13 18:06发布

问题:

I was wondering how can I add extra whitespace in php is it something like \s please help thanks.

Is there a tutorial that list these kind of things thanks.

回答1:

Like this:

 str_repeat(' ', 5); // adds 5 spaces


回答2:

To render more than one whitespace on most web browsers use   instead of normal white spaces.

echo "<p>Hello &nbsp;&nbsp;&nbsp; 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.

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)

Any way you do the output will be the same but the browser itself strips double white spaces and renders as one.



回答3:

PHP (typically) generates HTML output for a web-site.

When displaying HTML, the browser (typically) collapses all whitespace in text into a single space. Sometimes, between tags, it even collapses whitespace to nothing.

In order to persuade the browser to display whitespace, you need to include special tags like &nbsp; or <br/> in your HTML to add non-breaking whitespace or new lines, respectively.



回答4:

for adding space character you can use

<? 
echo "\x20\x20\x20"; 
 ?>


回答5:

Use str_pad function. It is very easy to use. One example is below:

<?php

$input = "Alien";
echo str_pad($input, 10);                      // produces "Alien     "

?>


回答6:

use this one. it will provide 60 spaces. that is your second parameter.

 echo str_repeat("&nbsp;", 60); 


回答7:

pre is your friend.

<pre> 
<?php // code goes here

?>
</pre>

Or you can "View Source" in your browser. (Ctrl+U for most browser.)



回答8:

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>


回答9:

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.

give output class wrappers and style accordingly.



回答10:

you can use the <pre> tag to prevent multiple spaces and linebreaks from being collapsed into one. Or you could use &nbsp; for a typical space (non-breaking space) and <br /> (or <br>) for line breaks.

But don't do <br><br><br><br> just use a <p> tag and adjust the margins with CSS.

<p style="margin-top: 20px;">Some copy...</p>

Although, you should define the styles globally, and not inline as I have done in this example.

When you are outputting strings from PHP you can use "\n" for a new line, and "\t" for a tab.

<?php echo "This is one line\nThis is another line"; ?>

Although, flags like \n or \t only work in double quotes (") not single wuotes (').



回答11:

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

this is my php variable foo
this is another php echo here foo


回答12:

when you add more space between double quotes or single quotes PHP takes only one white space ,so if you want to add more white space between words or strings use tab '\t' sequence

echo "\t";