首页 > 其他 > 详细

D. 读取文件中中文字符串的做法

时间:2017-12-25 00:27:48      阅读:257      评论:0      收藏:0      [点我收藏+]
<?php
    //$fname文件名称
$fname = "./c.txt";

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

?>

D. 读取文件中中文字符串的做法

原文:http://www.cnblogs.com/youyuanjuyou/p/8099439.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!