600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > php 写入文件编码 PHP写入到文本文件乱码

php 写入文件编码 PHP写入到文本文件乱码

时间:2020-10-01 10:40:05

相关推荐

php 写入文件编码 PHP写入到文本文件乱码

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'=>'打开文件失败'); }}

取消

评论

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。