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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Sat May 04 16:01:31 2024 UTC