php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43829 string padding doesn't work with default
Submitted: 2008-01-13 19:46 UTC Modified: 2008-01-13 23:29 UTC
From: l dot nanetti at rug dot nl Assigned:
Status: Not a bug Package: Strings related
PHP Version: 5.2.5 OS: windows
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: l dot nanetti at rug dot nl
New email:
PHP Version: OS:

 

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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-01-13 21:10 UTC] public dot chris at web dot de
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(' ', '&nbsp;', $fstring);
echo $fstring;

or replace echo $fstring; by following

echo '<pre>' . $fstring . '</pre>';
 [2008-01-13 23:29 UTC] felipe@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 17 05:01:34 2025 UTC