Skip to content

mb_detect_encoding does not pick first matching candidate #16566

@Lehren

Description

@Lehren

Description

The following code:

<?php
$encodings = [
            'UTF-8',
            'UTF-16',
            'UTF-32',
            'ISO-8859-1',
            'ISO-8859-2',
            'ISO-8859-6',
            'ISO-8859-15',
            'ASCII'
        ];
        
$a = 'NL';

echo mb_detect_encoding($a, $encodings, true);

Resulted in this output:

'UTF-16'

But I expected this output instead:

'UTF-8'

This worked correctly in 8.0, and was broken from that point onward. Just the string 'NL' is valid UTF-8. It's also valid UTF-16 of course, but UTF-8 is first in the list, so I expect it to be picked first. If the string contained UTF-16 characters, only then would I expect it to return 'UTF-16'

Sandbox: https://onlinephp.io/c/86885

PHP Version

PHP 8.3.12

Operating System

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions