php中用file_get_contents函数获取带BOM的utf-8文件内容

 

$dataStr = file_get_contents("C:\Users\Administrator\Desktop\\13.txt");//utf8文件

if (preg_match('/^\xEF\xBB\xBF/', $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

 

posted @ 2021-05-26 11:38  与f  阅读(318)  评论(0编辑  收藏  举报