Jump to content

Simple print_f() example from the php.net site won't display right.


Slips

Recommended Posts

Hello all, I was just reading the PHP manual and came across this example that doesn't work right for me. Anyone know why this is so? Tried looking through the manual for this but found nothing about it..

 

$s = 'monkey';
$t = 'many monkeys';

printf("[%s]\n",      $s); // standard string output
printf("[%10s]\n",    $s); // right-justification with spaces
printf("[%-10s]\n",   $s); // left-justification with spaces
printf("[%010s]\n",   $s); // zero-padding works on strings too
printf("[%'#10s]\n",  $s); // use the custom padding character '#'
printf("[%10.10s]\n", $t); // left-justification but with a cutoff of 10 characters

 

The above example will output:

 

[monkey]

[    monkey]

[monkey    ]

[0000monkey]

[####monkey]

[many monke]

 

 

 

 

 

But when i tried this for myself, 

printf("[%10s]\n",    $s);

does not left pad the output as so : [    monkey].Instead it outputs [ monkey].

 

But adding a 0 as so : 

printf("[%010s]\n",    $s);

, pads it with 4 0's, outputting :

[0000monkey] . Is there some other way to make it pad spaces that i'm missing?

 

Running PHP 5.3.2

 

Link to comment
Share on other sites

Are you viewing this in a web browser (likely, unknowingly, as HTML)? The padding is happening but you just can't see it.

 

Do one of the following:

  • Use "view source" in the browser to see what is really being output.
  • Send the text as plain text, using ini_set('default_mimetype', 'text/plain') or similar, instead of HTML.
  • Preserve the spacing, even in HTML, by wrapping the output in <pre> tags.

 

Link to comment
Share on other sites

Wow, all three ways you suggested worked great, but it doesn't seem to preserve the spaces when in text/html. Is this normal? I tried the same code on chrome as well, and got the same results (Padding was eaten away).

Link to comment
Share on other sites

Yes, it is entirely normal and you will see the same behaviour in all browsers.

 

There are technical reasons why, in HTML, the format of the document (how the HTML source code looks) is different from how it is rendered (how it looks in a browser). But all that you need to know is that multiple spaces are displayed as just one (unless you use something like <pre>) so the following all display in the same way (as Some text here).

 

  • Some                text            here
  • Some                                text here
  • Some text here

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.