PHP读取TXT中文乱码的解决方法
2017-09-13 18:51
585 查看
//$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;
}
}
}
}
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中文乱码的解决方法
- php读取xml中文乱码解决方法
- 读取txt的中文字符出现乱码的解决方法
- php读取mysql中文数据出现乱码的解决方法
- 读取txt的中文字符出现乱码的解决方法
- C# 读取 TXT文件的时候中文乱码解决方法
- JS读取PHP中设置的中文cookie时出现乱码的解决方法
- php 读取xml的方法 (iconv解决中文乱码问题)
- php读取mysql中文数据出现乱码的解决方法
- PHP读取TXT中文乱码的解决方式
- Windows下php页面读取 mysql"中文数据"出现乱码的解决方法
- iOS读取txt文件出现中文乱码的解决方法
- php读取mysql中文数据出现乱码的解决方法
- php读取目录中文文件名乱码解决方法
- c# 读取txt文件中文乱码解决方法
- phpexcel 读取中文gbk数据的csv在linux服务器下乱码的解决方法.
- php+AJAX传送中文会导致乱码的问题的解决方法
- PHP读写MYSQL中文乱码的解决方法
- PHP读MYSQL中文乱码的解决方法
- MySQL中文乱码,phpmyadmin乱码,php乱码 产生原因及其解决方法