//$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中文乱码的解决方法的更多相关文章

  1. linux下打开txt显示乱码的解决方法

    Linux打开txt文件乱码的解决方法   Linux显示在Windows编辑过的中文就会显示乱码是由于两个操作系统使用的编码不同所致.Linux下使用的编码是utf8,而Windows使用的是gb1 ...

  2. php 使用phpmailer 发送邮件(附带中文乱码的解决方法)

    下载phpmailer ,在程序里包含class.phpmailer.php 类  ,这里有中文乱码的解决方法 实例代码如下 <html> <head> <title&g ...

  3. xShell终端中文乱码完全解决方法

    xShell终端中文乱码完全解决方法 xShell(xShell5)以及其他终端中文乱码的原因无非有三种:(1)Linux系统的编码问题:(2)xShell终端的编码问题: (3)两端的语言编码不一致 ...

  4. 【IntelliJ IDEA】从资源文件读取出来就中文乱码的解决方法

    在application.properties资源文件中设置两个自定义的属性以及属性值: com.sxd.name = "德玛西亚" com.sxd.want = "王者 ...

  5. Qt 读取txt文件乱码的解决办法

    Qt 读取txt文本乱码问题 2015-05-20 15:46 方法一:使用QString的fromLocal8Bit()函数 复制代码 QFile txtfile(filePath);        ...

  6. .Net Core踩坑记:读取txt中文乱码

    迁移.net framework的项目,有块读取txt中文转码的问题,普通的不能再普通的代码,想都没想直接copy过去,也没测,结果今天就被坑了.Core是3.1版本,这是原来的代码: string ...

  7. Mysql命令行中文乱码的解决方法

    环境:Windows 8 64位,Mysql  5.0.96 for Win64 (x86) 数据库本身安装时默认已经是使用utf8编码的了,但在命令行中执行查询时,查询到的中文依然乱码,解决方法如下 ...

  8. 30. CentOS终端命令行显示中文乱码的解决方法

    安装CentOS的时候选择了中文,结果在终端不能显示中文,都是乱码,解决方法:修改/etc/sysconfig/i18n,内容为   代码如下: LANG="zh_CN.GB18030&qu ...

  9. windows下SVN日志反馈中文乱码的解决方法

    转自:https://shiyousan.com/post/635889908703806636 TortoiseSVN中文乱码的问题困扰了我好久,特别是每次使用“以标准差异文件显示修改”时,打开的文 ...

随机推荐

  1. CodeForces 835D - Palindromic characteristics | Codeforces Round #427 (Div. 2)

    证明在Tutorial的评论版里 /* CodeForces 835D - Palindromic characteristics [ 分析,DP ] | Codeforces Round #427 ...

  2. L1L2

    https://baijiahao.baidu.com/s?id=1595711904189222402&wfr=spider&for=pc https://www.cnblogs.c ...

  3. vue 日期格式化过滤器

    formateDate日期格式化,写在公共的js中: export function formateDate(date, fmt){ if ('/(y+)/'.test(fmt){ fmt = fmt ...

  4. ubunut:子进程 post-installation script 返回了错误号 1

    解决dpkg/apt-get error : 子进程 post-installation script 返回了错误号 1 这个不太好解决,感觉这个比较好,未测试 https://blog.csdn.n ...

  5. ec20 queclocator V1. 0 test

    AT+QICSGP=1,1,"UNIWAP","","",1 AT+QIACT=1 AT+QLOCCFG="contextid&q ...

  6. HGOI 20191106 题解

    Problem A  旅行者 有$n$种转移装置,每种转移装置本质相同,每种装置可以前进$a_i$单位,但只有$b_i$个. 从初始坐标为$0$出发,途中不能经过$c_1,c2,...,c_m$中的任 ...

  7. Selenium定位class包含空格的元素-复合class节点

    在HTML中, 节点有三种常见属性, 分别是id, name和class, 其中class是一个特殊的属性, 支持多个类名, 以空格隔开, 如下图所示: 你是否注意到, 为什么selenium中的fi ...

  8. SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xce in position 0: invalid continuatio

    点击  文档>>设置文件编码>>Unicode>>Unicode(UTF-8)

  9. AtCoder AGC001D Arrays and Palindrome (构造)

    补一下原来做过的AtCoder思维题的题解 题目链接: https://atcoder.jp/contests/agc001/tasks/agc001_d 先特判一些小的情况. 原题就相当于每个回文串 ...

  10. UTC ISO 8601

    如果时间在零时区,并恰好与协调世界时相同,那么(不加空格地)在时间最后加一个大写字母Z.Z是相对协调世界时时间0偏移的代号.如下午2点30分5秒表示为14:30:05Z或143005Z:只表示小时和分 ...