php / php/php-src

mb_check_encoding is detecting SJIS as Windows-1252 in PHP 8.1

Open
#13,746 14 comments 0 reactions 1 assignee View on GitHub

@alexdowad is already working on this.

Since Mar 22, 2024.

Bug Extension: mbstring Status: Needs Triage
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

Works as expected in 7.4

The following code:

<?php

$string = mb_convert_encoding('パーティーへ行かないか', 'SJIS', 'UTF-8');

var_dump(mb_check_encoding($string, 'SJIS')); // should be valid
var_dump(mb_check_encoding($string, 'Windows-1252')); // should be invalid

var_dump(mb_detect_encoding($string, 'SJIS', true)); // should be valid
var_dump(mb_detect_encoding($string, 'Windows-1252', true)); // should be invalid

var_dump(mb_convert_encoding($string, 'UTF-8', 'SJIS')); // should be correct
var_dump(mb_convert_encoding($string, 'UTF-8', 'Windows-1252')); // should be wrong

Resulted in this output:

bool(true)
bool(true)
string(4) "SJIS"
string(12) "Windows-1252"
string(33) "パーティーへ行かないか"
string(43) "ƒp[ƒeƒB[‚֍s‚©‚È‚¢‚©"

But I expected this output instead:

bool(true)
bool(false)
string(4) "SJIS"
bool(false)
string(33) "パーティーへ行かないか"
string(46) "ƒp�[ƒeƒB�[‚Ö�s‚©‚È‚¢‚©"

Online example here

PHP Version

PHP 8.1.27

Operating System

CentOS 7

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.