您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 商洛分类信息网,免费分类信息发布

php写入txt文件后字符乱码怎么办

2024/6/17 4:48:40发布8次查看
php写入txt文件后字符乱码的解决办法:1、创建一个php示例文件;2、创建“function file_utf8($filepath){...}”;3、将txt文件另存为“utf-8”即可。
本文操作环境:windows7系统、php7.1版,dell g3电脑
php写入txt文件后字符乱码怎么办?
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'=>'打开文件失败'); }}
推荐学习:《php视频教程》
以上就是php写入txt文件后字符乱码怎么办的详细内容。
商洛分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录