php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #66234 Printf %s counts bytes not characters
Submitted: 2013-12-04 12:50 UTC Modified: 2013-12-04 13:15 UTC
From: egorinsk at gmail dot com Assigned: krakjoe (profile)
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: Windows XP
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: egorinsk at gmail dot com
New email:
PHP Version: OS:

 

 [2013-12-04 12:50 UTC] 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.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-12-04 13:15 UTC] 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 13:15 UTC] krakjoe@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: krakjoe
 [2013-12-04 13:15 UTC] krakjoe@php.net
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.
 [2020-02-07 06:08 UTC] phpdocbot@php.net
Automatic comment on behalf of krakjoe
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=74684dc111fbc751ac4cbd3b720980b2bfd9382b
Log: fix #66234 add not about wchar and sprintf
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Sun Oct 11 22:00:01 2026 UTC