php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #81244 make *printf() function Unicode-aware
Submitted: 2021-07-11 10:21 UTC Modified: 2021-07-12 13:31 UTC
From: flightvision at googlemail dot com Assigned:
Status: Wont fix Package: *Unicode Issues
PHP Version: 8.0.8 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
35 - 28 = ?
Subscribe to this entry?

 
 [2021-07-11 10:21 UTC] flightvision at googlemail dot com
Description:
------------
The *printf() functions are having problems if strings contain non-ASCII characters.

Test script:
---------------
echo sprintf('%3.3s', 'ö12345');
echo sprintf('%3.3s', '12ö345');

Expected result:
----------------
ö12
12ö

Actual result:
--------------
ö1
12�

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-07-12 13:31 UTC] nikic@php.net
-Status: Open +Status: Wont fix
 [2021-07-12 13:31 UTC] nikic@php.net
printf() widths are in terms of bytes, and this isn't going to change. Please use mb_substr() or grapheme_substr() to truncate a string to a certain number of characters, depending on which notion of "character" you wish to use (I would recommend extended grapheme clusters, if you don't want to "truncate" away combining characters etc).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 13:01:30 2024 UTC