mbstring: Make encoding detection stricter #4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PHP 8.3 changed how source encoding detection works:
https://www.php.net/manual/en/migration83.other-changes.php#migration83.other-changes.functions.mbstring
Most locales only consider
ASCII
andUTF-8
(seemb_detect_order()
),and when a byte sequence invalid in both tested encodings (such as 0x91 for ‘ in Windows-1252) is encountered,
one of them might now be chosen as the most fitting encoding.
(This is done using the heuristics introduced in PHP 8.1:
php/php-src@28b346b)
Compare the output of the following script across PHP versions:
Let’s run the
mb_detect_encoding()
ourselves with$strict
argument set totrue
, to ensure consistent behaviour across all PHP versions.This might potentially cause a regression is some cases. Not sure.
Additionally, since we are now ensuring all encodings are valid, we can drop the warning capture mechanism.
It does not work on PHP ≥ 8.0 anyway, since that raises a
ValueError
instead of a warning when an invalid encoding is provided.https://www.php.net/manual/en/function.mb-convert-encoding.php#refsect1-function.mb-convert-encoding-errors
Also adjust the confusing string in tests.
https://www.php.net/manual/en/function.mb-convert-encoding.php
https://www.php.net/manual/en/function.mb-detect-encoding.php
https://www.php.net/manual/en/function.mb-detect-order.php