For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > October 2006 > printf not working









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author printf not working
Michael Shoemaker

2006-10-20, 6:56 pm

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?
IchBin

2006-10-20, 9:56 pm

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-)
Rik

2006-10-20, 9:56 pm

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


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com