Jump to content

Padding With Printf Not Displaying In Browser


SteveMurphy

Recommended Posts

I have PHP 5.5 with the Apache http 2.2 server running under Windows XPI have the following PHP script:

<html> <head>  <style> 	.mono {font-family: courier};  </style> </head>	 <body>  <h1>Padding and Justification</h1>  <?php	echo '<div class="mono">';	$s = 'monkey';	printf("[%-10s]",   $s); // left-justification with spaces	echo "<br/>";	echo '</div>';   ?> </body></html>

When I open the script in my browser, the output looks like this:

[monkey ]

But I think output should be four spaces before the ']' like this:

[monkey	]

The page source shows the four spaces, so the extra white space is being eliminated, and I believe that the is the correct browser behavior, but I would like to see the properly formatted output in my browswer. An idea occurred to me -- I could write output to a file, but I would prefer to view it in a browser.Does anyone know how to achieve my desired output?Thanks!

Link to comment
Share on other sites

Well, I figured out one solution:

$space = " ";$s = 'monkey';$line = sprintf("[%-10s]",   $s); // left-justification with spaces$line = str_replace(' ', $space, $line);echo $line;

Of course for reuse I can turn this into a function. Is there an easier way to do this?Thanks.

I have PHP 5.5 with the Apache http 2.2 server running under Windows XPI have the following PHP script:
<html> <head>  <style> 	.mono {font-family: courier};  </style> </head>	 <body>  <h1>Padding and Justification</h1>  <?php	echo '<div class="mono">';	$s = 'monkey';	printf("[%-10s]",   $s); // left-justification with spaces	echo "<br/>";	echo '</div>';   ?> </body></html>

When I open the script in my browser, the output looks like this:

[monkey ]

But I think output should be four spaces before the ']' like this:

[monkey	]

The page source shows the four spaces, so the extra white space is being eliminated, and I believe that the is the correct browser behavior, but I would like to see the properly formatted output in my browswer. An idea occurred to me -- I could write output to a file, but I would prefer to view it in a browser.Does anyone know how to achieve my desired output?Thanks!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...