分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

PHP读取TXT中文乱码的解决方法

发布时间:2023-09-06 01:07责任编辑:顾先生关键词:PHP乱码

  //$fname文件名称

   if ($fname = $_FILES[‘nickname‘][‘tmp_name‘]) {

          //file_get_contents() 函数把整个文件读入一个字符串中。

          $contents = file_get_contents($fname);

             //获取文件的编码方式

          $encoding = mb_detect_encoding($contents, array(‘GB2312‘,‘GBK‘,‘UTF-16‘,‘UCS-2‘,‘UTF-8‘,‘BIG5‘,‘ASCII‘));

          $fp=fopen($fname,"r");//以只读的方式打开文件

          $text = "";

          $num = 0;

          if(!(feof($fp))) {

              $num++;

              $str = trim(fgets($fp));

              if ($encoding != false) {

          //iconv ()将$encoding 转换成“UTF-8”

                  $str = iconv($encoding, ‘UTF-8‘, $str);

                  if ($str != "" and $str != NULL) {

                      $text = $str;

                  }

              }

              else {

                  $str = mb_convert_encoding ( $str, ‘UTF-8‘,‘Unicode‘);

                  if ($str != "" and $str != NULL) {

                      $text = $str;

                  }

              }

          }

          while(!(feof($fp))) {

              $str = ‘‘;

              $str = trim(fgets($fp));

              if ($encoding != false) {

                  $str = iconv($encoding, ‘UTF-8‘, $str);

                  if ($str != "" and $str != NULL) {

                      $text = $text.",".$str;

                  }

              }

              else {

                  $str = mb_convert_encoding ( $str, ‘UTF-8‘,‘Unicode‘);

                  if ($str != "" and $str != NULL) {

                      $text = $text.",".$str;

                  }

              }

          }

      }

 
 

PHP读取TXT中文乱码的解决方法

原文地址:http://www.cnblogs.com/daochong/p/7516732.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved