PHP读取TXT中文乱码的解决方式
2015-12-14 20:35
609 查看
因为业务上的需求,需要使用PHP读取一个TXT文件,但是在设计到中文的问题上,就遇到了恶心的乱码问题;
首先查看一下TXT的编码格式有四种:ANSI、Unicode、Unicode Big Endian、UTF-8
1、先是使用mb_detect_encoding($contents, array('GB2312','GBK','UTF-16','UCS-2','UTF-8','BIG5','ASCII'))语句
发现即使在其中增加了Unicode格式,已经无法获得文件的编码格式,但是对与ANSI和UTF-8格式倒是可以使用;
2、于是针对这个问题,专门做了一个如下的转换:
$str = mb_convert_encoding ( $str, 'UTF-8','Unicode');
只是需要在前面加上一个编码格式的判断。
完整代码如下:
if ($fname = $_FILES['nickname']['tmp_name']) {
//获取文件的编码方式
$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) {
$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;
}
}
}
}
注:本文转载自http://my.oschina.net/landry17h/blog/17145
首先查看一下TXT的编码格式有四种:ANSI、Unicode、Unicode Big Endian、UTF-8
1、先是使用mb_detect_encoding($contents, array('GB2312','GBK','UTF-16','UCS-2','UTF-8','BIG5','ASCII'))语句
发现即使在其中增加了Unicode格式,已经无法获得文件的编码格式,但是对与ANSI和UTF-8格式倒是可以使用;
2、于是针对这个问题,专门做了一个如下的转换:
$str = mb_convert_encoding ( $str, 'UTF-8','Unicode');
只是需要在前面加上一个编码格式的判断。
完整代码如下:
if ($fname = $_FILES['nickname']['tmp_name']) {
//获取文件的编码方式
$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) {
$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;
}
}
}
}
注:本文转载自http://my.oschina.net/landry17h/blog/17145
相关文章推荐
- php单例模式学习
- mac下配置matplotlib绘图
- ThinkPHP--多表查询之join和table的用法
- PHP防csrf攻击
- php代码下载文件转换成迅雷,快车,qq下载文件源代码
- 发送电子邮件模块smtplib
- EditPlus 注册码在线生成
- PHP number_format()函数
- php创建二维码
- 远程管理数据库phpMyAdmin部署方案
- PHPCMS V9 按浏览次数排行调用文章
- 用Matplotlib绘制股票每月最高收盘价曲线
- php内核探索-常量
- 去掉 phpwind 8.7 敏感词提示
- PHP多进程处理并行处理任务实例
- WordPress中访客登陆实现邮件提醒的PHP脚本实例分享
- php开发客服系统(持久连接+轮询+反向ajax)
- php中并发读写文件冲突的解决方案
- php 实现消息队列
- sublime text3 自动编译php 适合用于简单的php文件执行