Slips Posted December 7, 2010 Share Posted December 7, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/220915-simple-print_f-example-from-the-phpnet-site-wont-display-right/ Share on other sites More sharing options...
salathe Posted December 7, 2010 Share Posted December 7, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/220915-simple-print_f-example-from-the-phpnet-site-wont-display-right/#findComment-1143936 Share on other sites More sharing options...
Slips Posted December 7, 2010 Author Share Posted December 7, 2010 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). Quote Link to comment https://forums.phpfreaks.com/topic/220915-simple-print_f-example-from-the-phpnet-site-wont-display-right/#findComment-1143943 Share on other sites More sharing options...
salathe Posted December 7, 2010 Share Posted December 7, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/220915-simple-print_f-example-from-the-phpnet-site-wont-display-right/#findComment-1143959 Share on other sites More sharing options...
Slips Posted December 7, 2010 Author Share Posted December 7, 2010 I see, that explains all those times my spaces wouldn't display, thanks a lot for clearing that up. Quote Link to comment https://forums.phpfreaks.com/topic/220915-simple-print_f-example-from-the-phpnet-site-wont-display-right/#findComment-1143982 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.