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.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3628e-2c005.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3628e-2c005.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?