开发者

How to add extra whitespace in PHP?

开发者 https://www.devze.com 2022-12-20 22:04 出处:网络
I was wondering how can I add extra whitespace in php is it 开发者_如何学编程something like \\s please help thanks.

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.


Like this:

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


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 (I don't know, but this one)

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


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.


for adding space character you can use

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


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

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


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

<?php

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

?>


pre is your friend.

<pre> 
<?php // code goes here

?>
</pre>

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


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>


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.


You can also use this

str_repeat("\x20", $numberOfRepeats);


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


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


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";


PHP is an easy language with multiple solutions. A Quick Solution would be using Double Quotes " ". Example Below.

$var1 = "Hello";
$var2 = "World";
$var3 = "How";
$var4 = "are";
$var5 = "you";
$var6 = "?";
$var7 = ",";

echo "$var1 $var2$var7 $var3 $var4 $var5$var6";

//Output: Hello World, How are you?
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号