Bug #63079 [Opn]: String access by character is not multibyte-safe

From: Date: Thu, 13 Sep 2012 11:57:39 +0000
Subject: Bug #63079 [Opn]: String access by character is not multibyte-safe
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-173407@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=63079&edit=1 ID: 63079 User updated by: astatutov at gmail dot com Reported by: astatutov at gmail dot com Summary: String access by character is not multibyte-safe Status: Open Type: Bug Package: Strings related PHP Version: Irrelevant Block user comment: N Private report: N New Comment: > you should use mb_* to deal with multi-byte characters I know it. I mentioned it in the description. The option mbstring.func_overload do it for me. But bracket operator is still unusable: the documentation states it accesses the character while it doesn't. And I believe it's not the documentation problem. Any modern language I know which is able to work with utf-8 do it transparently for developer. The aim of mbstring is the same, isn't it? Setting mbstring.internal_encoding to utf-8 a developer will expect that INTERNAL string accessing operator will support it. This is what the term "predictable behavior" means. Previous Comments: ------------------------------------------------------------------------ [2012-09-13 10:48:25] laruence@php.net yeah, it's not. you should use mb_* to deal with multi-byte characters ------------------------------------------------------------------------ [2012-09-13 09:58:55] astatutov at gmail dot com Description: ------------ I know, there is section named "Details of the String Type" in documentation. But still there is other section, that stats "Think of a string as an array of characters for this purpose". This is very convenient to think so. We use mbstring extension to work entirely on utf-8 and mbstring.func_overload option allows us almost forget about differences between regular and multibyte strings. We just write our application, thinking about its native logic, not PHP internal logic. This is high-level programming language, by the way. We're using strlen, substr, etc. as we're doing with regular strings. And BANG! String bracket operator returns bytes, not characters! I think it's unpredictable behavior, even if it's well-documented (but it's not). Considering that the use of utf-8 grows everywhere and maybe even PHP 6 will support it by default, why not implement multibyte support in bracket operations now in mbstring extension? Of course, it must be configurable to be back-compatible. I know, we can use substr as a replace of string accessing operation, but it's very slow and it's wrong in general. Also I now this is not a first bug on this subject. There was #51919 as example, which was closed and marked as not a bug. But I propose to look at this problem from the point of view of the language logic, not the implementation. Sorry, if I've missed something else. Test script: --------------- $str = "Kąt"; echo $str[1]; Expected result: ---------------- ą Actual result: -------------- � ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=63079&edit=1

« previous php.bugs (#173407) next »