Doc #66234 [NEW]: Printf %s counts bytes not characters

From: Date: Wed, 04 Dec 2013 12:50:29 +0000
Subject: Doc #66234 [NEW]: Printf %s counts bytes not characters
Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-10682@lists.php.net to get a copy of this message
From: egorinsk at gmail dot com Operating system: Windows XP PHP version: Irrelevant Package: Documentation problem Bug Type: Documentation Problem Bug description:Printf %s counts bytes not characters Description: ------------ The description for flag '%s' says: > An optional number, a width specifier that says how many characters (minimum) this conversion should result in. This is incorrect. The number specifies bytes count, not characters. With multibyte encodings such as utf-8, the right aligned text gets aligned wrong. And sprintf doesn't have an option to specify encoding so it cannot calculate text width anyway. I think there should be something like a red warning saying that width specifier for %s will not work correctly with utf-8 (which is very popular encoding) and would be better not used at all. I have tested the script on Windows and on Debian Linux (with LANG=en_US.UTF-8) and it works incorrectly on both systems. --- From manual page: http://www.php.net/function.sprintf --- Test script: --------------- <?php // The strings should be aligned at the same width printf("%10s\n%10s\n%10s\n", "Привет", "Hello", "おはよう"); Expected result: ---------------- Text in all strings is aligned to the right and every line has at least 10 characters: Привет Hello おはよう Actual result: -------------- Non-latin text is aligned to the left Привет Hello おはよう The string with text «Привет» has only 6 characters (that take 12 bytes in utf-8) and is not padded with 4 spaces as it should be. -- Edit bug report at https://bugs.php.net/bug.php?id=66234&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=66234&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=66234&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=66234&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=66234&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=66234&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=66234&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=66234&r=needscript Try newer version: https://bugs.php.net/fix.php?id=66234&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=66234&r=support Expected behavior: https://bugs.php.net/fix.php?id=66234&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=66234&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=66234&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=66234&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=66234&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=66234&r=dst IIS Stability: https://bugs.php.net/fix.php?id=66234&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=66234&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=66234&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=66234&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=66234&r=mysqlcfg

« previous php.doc.bugs (#10682) next »