php's method of determining whether a Web page is utf8 encoded

  • 2021-06-28 12:03:26
  • OfStack

//Decision Encoding

$encode = mb_detect_encoding($q, array('GB2312','GBK','UTF-8'));
echo $encode."<br/>";
if($encode=="GB2312")
{
    $q = iconv("GBK","UTF-8",$q);
}
else if($encode=="GBK")
{
    $q = iconv("GBK","UTF-8",$q);
}
else if($encode=="EUC-CN")
{
    $q = iconv("GBK","UTF-8",$q);
}
else//CP936
{
    //$q = iconv("GB2312","UTF-8",$q);
}


It's actually using mb_detect_encoding function, if not utf8 encoding, is converted to utf8 encoding to prevent chaotic encoding.

Related articles: