The mb_detect_encoding () function is inaccurate.

I want to dynamically obtain the coding set of a character through the mb_detect_encoding () function, and then convert it through iconv, but the encoding type judged by mb_detect_encoding () is incorrect, resulting in iconv () invalid or garbled.

//
if (is_string($k)) {
    $encoding = getEncoding($k);
    $k = iconv($encoding, "utf-8", $k);
}
//
function getEncoding($data)
{
   return mb_detect_encoding($data, array("ASCII","GB2312","GBK","BIG5","UTF-8"));
}
//
$k = "";
$encoding = getEncoding($k);
var_dump($encoding,$k);
$k = iconv($encoding, "utf-8", $k);
var_dump(getEncoding($k),$k);exit;

//:string(5) "CP936" string(6) "" string(5) "CP936" string(9) ""
Mar.31,2021

mb_detect_encoding-Encoding of detected characters

string mb_detect_encoding (string $str [, mixed $encoding_list = mb_detect_order () [, bool $strict = false]])

it can be seen that the second parameter is not required. If $encoding_list is omitted, the character set will be automatically obtained using mb_detect_order (), so the code can be adjusted as follows:

   
Brother, I'm not trying to flatter you. For example, I typed a string of English, got the character set result as ASCII, then converted it to utf-8 via iconv, and should return utf-8 later. Why is it still the ASCII returned? I am rather confused about this point.

Menu