php写入txt文件后字符乱码的解决办法:1、创建一个PHP示例文件;2、创建“function file_utf8($filepath){...}”;3、将txt文件另存为“UTF-8”即可。

本文操作环境:windows7系统、PHP7.1版,DELL G3电脑
php写入txt文件后字符乱码怎么办?
php处理中文编码老是有问题,这是编码的问题,可以将txt文件另存为UTF-8的编码再处理;
参考如下:
立即学习“PHP免费学习笔记(深入)”;
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'=>'打开文件失败');
}
}推荐学习:《PHP视频教程》
以上就是php写入txt文件后字符乱码怎么办的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号