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) ""