|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-01-13 19:46 UTC] l dot nanetti at rug dot nl
Description: ------------ what I want to do: left-padding a string with spaces. In this example, up to 30 charachters. code snippet: <?php $mystring = 'Luca Nanetti'; $formatstring = "[%30s]"; $fstring = sprintf($formatstring, $mystring); echo $fstring; ?> expected: [ Luca Nanetti] what I get is instead a one-charachter padding, i.e. the string is left padded with only one space: [ Luca Nanetti] it successfully works with the number 0: using "[%030s]" I get [000000000000000000Luca Nanetti] it successfully works with custom padding, "[%'#30s]" Additional informations: - Abyss web server, accessed locally on 127.0.0.1:8000 - modules: PDO, PDO_SQLITE, MCRYPT Reproduce code: --------------- <?php $mystring = 'Luca Nanetti'; $formatstring = "[%30s]"; $fstring = sprintf($formatstring, $mystring); echo $fstring; ?> Expected result: ---------------- [ Luca Nanetti] Actual result: -------------- [ Luca Nanetti] PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 16 23:00:01 2025 UTC |
isn' t a bug. browser shows just one space. look at the source. use the following code using htmlentities $mystring = 'Luca Nanetti'; $formatstring = "[%30s]"; $fstring = sprintf($formatstring, $mystring); $fstring = str_replace(' ', ' ', $fstring); echo $fstring; or replace echo $fstring; by following echo '<pre>' . $fstring . '</pre>';