This is a discussion on printf not working within the alt.comp.lang.php forums, part of the PHP Programming Forums category; per the php.net site... Example 6. printf(): string specifiers <?php $s = 'monkey'; $t = 'many monkeys'; printf("[%s]\...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
per the php.net site...
Example 6. printf(): string specifiers <?php $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 printout of this program would be: [monkey] [ monkey] [monkey ] [0000monkey] [####monkey] [many monke] However, on my system, this is what I get... [monkey] [ monkey] [monkey ] [0000monkey] [####monkey] [many monke] Am I missing something? |
|
|||
|
Michael Shoemaker wrote:
> per the php.net site... > > Example 6. printf(): string specifiers > <?php > $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 printout of this program would be: > > [monkey] > [ monkey] > [monkey ] > [0000monkey] > [####monkey] > [many monke] > > > However, on my system, this is what I get... > > [monkey] [ monkey] [monkey ] [0000monkey] [####monkey] [many monke] > > > > Am I missing something? You could do this: $b = '<br>'; printf("[%s]$b", $s); // standard string output -- Thanks in Advance... http://ichbinquotations.awardspace.com IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us __________________________________________________ ____________________ 'If there is one, Knowledge is the "Fountain of Youth"' -William E. Taylor, Regular Guy (1952-) |
|
|||
|
Michael Shoemaker wrote:
> per the php.net site... > > Example 6. printf(): string specifiers > <?php > $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 printout of this program would be: > > [monkey] > [ monkey] > [monkey ] > [0000monkey] > [####monkey] > [many monke] > > > However, on my system, this is what I get... > > [monkey] [ monkey] [monkey ] [0000monkey] [####monkey] [many monke] > > > > Am I missing something? Yes, you work in a browser with ignores newlines. Check the source of your page, it should all be there. While using a browser for viewing \n, either: - wrap it in a <pre></pre> tag (preferred), - use printf("[%s]<br>",$s), - or cache the output like echo nl2br(sprintf(ntf("[%s]\n",$s); Option 1 will also not concate all spaces to 1. Grtz, -- Rik Wasmus |