$dataStr = file_get_contents("C:UsersAdministratorDesktop\13.txt");//utf8文件 if (preg_match('/^xEFxBBxBF/', $dataStr)) {//x表示16进制 $dataStr = substr($dataStr, 3);//把文件的BOM头过滤 } echo $dataStr ; $encode = mb_detect_encoding($dataStr, array("ASCII","UTF-8","GB2312","GBK","BIG5"));//判断内容的字符编码 echo $encode; if($encode == "UTF-8"){ $dataStr = iconv("UTF-8","GBK",$dataStr);//把utf8编码转为GBK } echo $dataStr ;
参: https://www.cnblogs.com/hf8051/p/4600329.html