#50851 [NEW]: sprintf's %s doesn't work correctly
| From: | mmaning at practiceinsight dot net | Date: | Wed, 27 Jan 2010 00:03:32 +0000 |
| Subject: | #50851 [NEW]: sprintf's %s doesn't work correctly | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-146967@lists.php.net to get a copy of this message | ||
From: mmaning at practiceinsight dot net
Operating system: Linux
PHP version: 5.2.12
PHP Bug Type: Output Control
Bug description: sprintf's %s doesn't work correctly
Description:
------------
The %s function in sprintf works just like all of the other % commands -
except when you attempt to restrict how large the string is. See the code
below. Also, this happens for printf as well.
It also happens under Linux and Windows.
Since %22c doesn't work because %c only does a single character (it
returns a null string when trying to use %22c), only %22s can be used. As
can be seen from the first of the two lines, when the string passed in is
less than 22 characters - it is padded to be 22 characters long. Thus
negating anyone's wish to say "The %s function isn't supposed to be used
like that." If it wasn't, then the first occurrence would have simply
generated "(--------------------)" instead of what it did.
Reproduce code:
---------------
<?php
$s = str_repeat( "-", 20 );
echo sprintf( "My String (%22s) is 22 characters long.\n", $s );
$s = str_repeat( "-", 40 );
echo sprintf( "My String (%22s) is 22 characters long.\n", $s );
exit( 0 );
$s = str_repeat( "-", 20 );
printf( "My String (%22s) is 22 characters long.\n", $s );
$s = str_repeat( "-", 40 );
printf( "My String (%22s) is 22 characters long.\n", $s );
exit( 0 );
?>
Expected result:
----------------
My String ( --------------------) is 22 characters long.
My String (----------------------) is 22 characters long.
My String ( --------------------) is 22 characters long.
My String (----------------------) is 22 characters long.
Actual result:
--------------
My String ( --------------------) is 22 characters long.
My String (----------------------------------------) is 22 characters
long.
My String ( --------------------) is 22 characters long.
My String (----------------------------------------) is 22 characters
long.
--
Edit bug report at http://bugs.php.net/?id=50851&edit=1
--
Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=50851&r=trysnapshot52
Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=50851&r=trysnapshot53
Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=50851&r=trysnapshot60
Fixed in SVN: http://bugs.php.net/fix.php?id=50851&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=50851&r=needdocs
Fixed in release: http://bugs.php.net/fix.php?id=50851&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=50851&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=50851&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=50851&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=50851&r=support
Expected behavior: http://bugs.php.net/fix.php?id=50851&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=50851&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=50851&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=50851&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=50851&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=50851&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=50851&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=50851&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=50851&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=50851&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=50851&r=mysqlcfg