zoukankan      html  css  js  c++  java
  • PHP读取TXT中文乱码的解决方法

      //$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;

                      }

                  }

              }

          }

     
     
  • 相关阅读:
    移动端跨平台方案对比:React Native、weex、Flutter
    js json处理 双引号
    jquery 发get post请求
    librtmp编译for android and ios 不要openssl
    mysql reset password重置密码
    pdf ppt word office转图片 教学白板
    使用阻塞队列实现生产者-消费者模型
    ThreadPoolExecutor线程池的分析和使用
    HashMap HashTable和ConcurrentHashMap的区别
    HTTP协议GET和POST请求的区别
  • 原文地址:https://www.cnblogs.com/daochong/p/7516732.html
Copyright © 2011-2022 走看看