sprintf, printf, etc formatting blurb<

liunx

Guest
ok, ive been reading the php manual for the past hour ...still cant figure this out ...

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


im trying to figure out how to print a maximum amount of letters .... yes, the answer is right there in my face, but i dont understand it.

so far, i understand this one:

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

the ' signifies you want all spaces to be filled in with # (or whatever character you want).

what is the 10s portion? 10 spaces? does the s stand for string? like, its expecting a string? actually, i found it at the top of the manual page. its how it treats it.

what i dont understand in some of them is the use of the period, as in the example of the maximum amount of characters thats left justified

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

10.10s ?? i understand the 10 to be the maximum amount of characters since its a string ..but the 10. ??

i dont even understand how its left justified ... above in the manual:

"3. An optional alignment specifier that says if the result should be left-justified or right-justified. The default is right-justified; a - character here will make it left-justified."

there is no - character in the format

any help would be great...

i just want a string outputted with a max of 20 characters followed by a ...

printf("%10.10s...", $t);

that is all i know to do ... just dont understand the 10.

but if anyone can make the whole print format function more clear for me, that would be great, cuz im somewhat confused ...I'm not sure if I'm understanding this correctly. From what I'm reading, I see that you have a string, and you want it to only print a certain number of letters in a string no matter how much bigger the string length is than your maximum. I don't think that I'm understanding it correctly, but if I am, could you not use the substr() function?

substr($string, 0, $max);

and then print the result?

For my sake, could you explain a little bit more about what you're trying to do?i don't use printf or sprintf - i use the string functions like substr and so on... much more powerful, and easier to figure things out.


however - to answer your questions:

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

%s - means "insert string here" (which would be the variable $s).

%10s - means "insert string here with a right justification 10 characters long". If your string is less than 10 characters long, it will pad with space characters.

%'#10s - means "insert string here with a right justification 10 characters long". If your string is less than 10 characters long, it will pad with # - you can use any character for padding, specified after the '



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

this means that you have a maximum output length of 10 characters, but if you have less than 10 characters it will be padded with a space character...
 
Back
Top