Doc #66234 [Opn->Csd]: Printf %s counts bytes not characters

From: Date: Wed, 04 Dec 2013 13:15:25 +0000
Subject: Doc #66234 [Opn->Csd]: Printf %s counts bytes not characters
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-10684@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=66234&edit=1 ID: 66234 Updated by: krakjoe@php.net Reported by: egorinsk at gmail dot com Summary: Printf %s counts bytes not characters -Status: Open +Status: Closed Type: Documentation Problem Package: Documentation problem Operating System: Windows XP PHP Version: Irrelevant -Assigned To: +Assigned To: krakjoe Block user comment: N Private report: N New Comment: This bug has been fixed in the documentation's XML sources. Since the online and downloadable versions of the documentation need some time to get updated, we would like to ask you to be a bit patient. Thank you for the report, and for helping us make our documentation better. It is correct, it would be confusing if it said byte count, what does that mean when applied to %05d. A warning box has been added with relevant advice. Previous Comments: ------------------------------------------------------------------------ [2013-12-04 13:15:04] krakjoe@php.net Automatic comment from SVN on behalf of krakjoe Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=332262 Log: fix #66234 add not about wchar and sprintf ------------------------------------------------------------------------ [2013-12-04 12:50:28] egorinsk at gmail dot com 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 this bug report at https://bugs.php.net/bug.php?id=66234&edit=1

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