Bug #81390 [Com]: mb_detect_encoding() regression
| From: | adrien dot foulon at tukan dot hu | Date: | Wed, 18 Jan 2023 12:38:44 +0000 |
| Subject: | Bug #81390 [Com]: mb_detect_encoding() regression | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-243483@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=81390&edit=1
ID: 81390
Comment by: adrien dot foulon at tukan dot hu
Reported by: alec at alec dot pl
Summary: mb_detect_encoding() regression
Status: Closed
Type: Bug
Package: mbstring related
PHP Version: 8.1.0beta5
Assigned To: alexdowad
Block user comment: N
Private report: N
New Comment:
This issue is still present in both 8.1 any version and 8.2 any version
Here is a snippet demonstrating the difference of execution between those versions
```php
# ensure that string is utf8
function fvm_ensure_utf8($str) {
$enc = mb_detect_encoding($str, mb_list_encodings(), true);
var_dump($enc);
if ($enc === false){
return false; // could not detect encoding
} else if ($enc !== "UTF-8") {
return mb_convert_encoding($str, "UTF-8", $enc); // converted to utf8
} else {
return $str; // already utf8
}
# fail
return false;
}
$css = 'input[type="radio"]:checked + img {
border: 5px solid #0083ca;
}';
$css = fvm_ensure_utf8($css);
echo $css;
```
You can run it on phpsandbox with version 8.0 8.1 and 8.2, 8.0 will output UTF8, 8.1+ will output
UTF7 and the following mb_convert_encoding drops the + in the string
Previous Comments:
------------------------------------------------------------------------
[2021-12-07 14:30:20] alec at alec dot pl
So, this is another case where detecting the charset is not possible.
I think we can close the ticket.
In my software I'm going to not depend on charset detection at all.
------------------------------------------------------------------------
[2021-12-07 12:07:39] alexdowad@php.net
Hi, Alec. OK, I tried converting the text to ISO-8859-2.
PHP 7 returns "ISO-8859-1". My new code for encoding detection returns
"Windows-1252". Why the difference?? I did a bit of analysis, and found...
PHP 7 was not able to auto-detect Windows-1252 at all. PHP 8.1 is. However, the heuristics which it
uses cannot tell any difference between ISO-8859-{1,2} or Windows-1252 in this case... so it returns
the one which appears earlier in the list. In this case, you put Windows-1252 earlier.
Let me know if you have any comments.
------------------------------------------------------------------------
[2021-12-06 07:01:37] alec at alec dot pl
That's better, but did you convert it to iso-8859-2 and then try to detect?
------------------------------------------------------------------------
[2021-12-05 16:01:08] alexdowad@php.net
Hi, Alec. Please keep up the great work with testing. I tried the text which you linked to:
---------------
$text = "Ola szykuje siÄ do szkoÅy. Jest już w piÄ
tej klasie. Dawniej obawiaÅa
siÄ szkoÅy, teraz bardzo lubi tam chodziÄ. W szkole nie tylko uczy siÄ ciekawych rzeczy
â spotyka też swoich kolegów i koleżanki. Najbardziej lubi spÄdzaÄ czas ze
swoimi przyjacióÅmi z klasy â sÄ
to Monika i MichaÅ.
Ola lubi wszystkie przedmioty. Wie, że nauka jest ważna. Najmilej spÄdza czas na lekcjach o
przyrodzie â Ola bardzo lubi zwierzÄta. W klasie Oli mieszka chomik. Wszystkie dzieci
dbajÄ
o niego. PrzynoszÄ
mu jedzenie i gÅaszczÄ
. Ola nie ma wÅasnego zwierzÄcia,
wiÄc chomik to kolejny powód dla którego lubi chodziÄ do szkoÅy.
SzkoÅa Oli jest blisko jej domu. Ola chodzi do szkoÅy sama. Gdy Ola byÅa mÅodsza â
zawsze odprowadzaÅ jÄ
tam tata, mama lub babcia. Przed każdym dniem szkoÅy Ola pakuje
plecak. Sprawdza czy sÄ
wszystkie ksiÄ
żki i zeszyty. Sprawdza też piórnik â czy
jest tam linijka, dÅugopis, oÅówek, gumka i mazaki.
W przerwie miÄdzy lekcjami Ola je w szkole obiad. DziÄki temu ma dużo siÅy na dalszÄ
naukÄ. Po szkole droga do domu jest krótka, ale zajmuje Oli dużo czasu. Dlaczego? Bo to
czas, w którym lubi rozmawiaÄ z koleżankami i kolegami podczas spaceru do domu. Ola jednak
wie, że nie może wróciÄ późno.";
echo mb_detect_encoding($text,'utf-8,windows-1252,iso-8859-1,iso-8859-2', true),
"\n";
-------------
And then, in my shell:
-----------------
17:56 ~prog/php/php-src % ./sapi/cli/php test.php
UTF-8
-----------------
Looks good to me! That's on the tip of my development branch, which (crucially) does include
the latest patch for Polish... the one which you said you didn't use.
Please do your best to see if you can still find breakage anywhere! If you find anything else which
is arguably a bug, I will be very, very happy to fix it!
------------------------------------------------------------------------
[2021-11-30 12:28:24] cmb@php.net
Related To: Bug #81676
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=81390
--
Edit this bug report at https://bugs.php.net/bug.php?id=81390&edit=1