printf not working

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]\...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-20-2006
Michael Shoemaker
 
Posts: n/a
Default printf not working

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?
Reply With Quote
  #2 (permalink)  
Old 10-21-2006
IchBin
 
Posts: n/a
Default Re: printf not working

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-)
Reply With Quote
  #3 (permalink)  
Old 10-21-2006
Rik
 
Posts: n/a
Default Re: printf not working

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


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 01:51 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0