SteveMurphy Posted February 13, 2010 Share Posted February 13, 2010 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 More sharing options...
SteveMurphy Posted February 13, 2010 Author Share Posted February 13, 2010 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 More sharing options...
justsomeguy Posted February 15, 2010 Share Posted February 15, 2010 If you're just debugging and want to view everything in the browser, print your output between <pre> tags. Link to comment Share on other sites More sharing options...
Synook Posted February 16, 2010 Share Posted February 16, 2010 You can use the CSS3 value white-space-collapse:preserve to achieve pre-formatted functionality for any selector, however as with anything CSS3 don't expect perfect compatibility. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.