[php-src] Issue #7871: `mb_detect_encoding()` detects UTF-8 emoji byte sequence as ISO-8859-1 since PHP 8.1
| From: | alexdowad | Date: | Sun, 02 Jan 2022 20:38:20 +0000 |
| Subject: | [php-src] Issue #7871: `mb_detect_encoding()` detects UTF-8 emoji byte sequence as ISO-8859-1 since PHP 8.1 | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-238716@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/7871
Comment Author: alexdowad
Hmm. One point to start:
Legacy versions of PHP return
UTF-8, not because they are smart enough to tell that the
string is actually UTF-8 text, but because you put UTF-8 first in the list of candidate encodings.
If you put ISO-8859-1 first, then all versions return ISO-8859-1.
The string is actually valid under both encodings; if interpreted as ISO-8859-1, it's an
accented "o", followed by an accented "Y", followed by a Yen sign, followed by a
superscript digit 3. A funny combination, but not invalid.
The current detection code treats all codepoints higher than U+FFFF as "rare", and
penalizes candidate encodings under which the string contains such codepionts. That is what is
making it prefer ISO-8859-1 over UTF-8.
@nikic Do you think I need to teach it that some emoji over U+FFFF are not "rare"?
Another point... no matter what we do, encoding detection will always be very, very unreliable for
*short* strings. If you want to auto-detect text encoding with any level of reliability, you really
need to give it more input text to work with.
Even so, let us think a bit about this issue of emoji. It is a fact that some emoji are used by a
lot of people, and they do occupy ranges of Unicode codepoints over U+FFFF.