Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The order of $encodings parameter in mb_detect_encoding() is sometimes ignored #1708

Open
machitgarha opened this issue Jul 20, 2022 · 3 comments · May be fixed by #2426
Open

The order of $encodings parameter in mb_detect_encoding() is sometimes ignored #1708

machitgarha opened this issue Jul 20, 2022 · 3 comments · May be fixed by #2426
Labels
bug Documentation contains incorrect information Extension: mbstring Status: Needs Triage

Comments

@machitgarha
Copy link

Description

The PHP documentation says:

$encodings

A list of character encodings to try, in order. The list may be specified as an array of strings, or a single string separated by commas.

But in PHP 8.1, it may not be regarded.

The following code:

echo mb_detect_encoding('$x + 2', ['UTF-7', 'UTF-8']) . PHP_EOL;
echo mb_detect_encoding('$x + 2', ['UTF-8', 'UTF-7']) . PHP_EOL;

Resulted in this output:

UTF-7
UTF-7

But I expected this output instead:

UTF-7
UTF-8

Maybe related to php/php-src#9008, but I felt like opening a new one is better.

PHP Version

PHP 8.1.8

Operating System

Fedora Workstation 36

@machitgarha machitgarha added bug Documentation contains incorrect information Status: Needs Triage labels Jul 20, 2022
@damianwadley
Copy link
Member

A little of 9008, but also php/php-src#7871 and php/php-src#8279: the change in behavior is intentional as heuristics play more of a role now than they did before, which can result in the function "preferring" one encoding over another regardless of their relative positions in the encodings list.

@damianwadley damianwadley transferred this issue from php/php-src Jul 20, 2022
@maiermic
Copy link

This seems to be a duplicate of #1490, but it has a more detailed description including examples.

@jspaetzel
Copy link

I tripped over this yesterday with one of my implementations of mb_detect_encoding. Created a PR to update docs #2426

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Documentation contains incorrect information Extension: mbstring Status: Needs Triage
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants