Heim > Fragen und Antworten > Hauptteil
Der mit mb_detect_encoding erhaltene Rückgabewert ist cp936. Entspricht dies GBK?
Nach der Transkodierung durch mb_convert_encoding wird der Text zwar normal angezeigt, aber wenn mb_detect_encoding zur Erkennung des Textkodierungsformats verwendet wird, ist er immer noch cp936 und hat sich nicht geändert. Warum ist das so?
Der Code lautet wie folgt:
$file_contents = fread($file,$fileSize);
$typeofData = mb_detect_encoding($file_contents,array("GBK","GB2312","UTF-8","ASCII","BIG5"));
if ($typeofData != "UTF-8"){
// $file_contents = iconv("GBK","UTF-8",$file_contents);
$file_contents = mb_convert_encoding($file_contents,"UTF-8","GBK");
}
echo mb_detect_encoding($file_contents,array("GBK","GB2312","UTF-8","ASCII","BIG5"))."<br/>";
echo $file_contents;
伊谢尔伦2017-05-16 13:16:00
GBK的Code Page是CP936.
我在Ubuntu上用PHP5和PHP7都试过了,转成UTF-8编码后能够检测到UTF-8:
<?php
$str = file_get_contents('/path/to/gbk.txt'); //GBK编码的文本文件
$order = array('GB2312', 'GBK', 'GB18030', 'UTF-8', 'ASCII', 'BIG5');
$encode = mb_detect_encoding($str, $order, true); //可见CP936(即GBK)
$str = mb_convert_encoding($str, 'UTF-8', $encode); //转成UTF-8
echo mb_detect_encoding($str, $order, true); //输出UTF-8