php处理中文编码老是有问题,这是编码的问题,可以将txt文件另存为UTF-8的编码再处理;
参考如下:
function file_utf8($filepath){ $f_contents= file_get_contents($filepath); $encoding = mb_detect_encoding($f_contents, array('GB2312','GBK','UTF-16','UCS-2','UTF-8','BIG5','ASCII')); $content_u=""; $handle=fopen($filepath,"r"); if ($handle){ while (!feof($handle)) { $buffer= fgets($handle); if ($encoding != false) { if (mb_detect_encoding($buffer)!='UTF-8'){ $buffer = iconv($encoding, 'UTF-8', $buffer); } }else{ $buffer = mb_convert_encoding ( $buffer, 'UTF-8','Unicode'); } $content_u.=$buffer; } fclose($handle); return $info=array('status'=>1,'message'=>$content_u); }else{ return $info=array('status'=>0,'message'=>'打开文件失败'); }}
取消
评论