读取csv文件时的字符编码问题 [英] Character encoding problems with reading a csv file
本文介绍了读取csv文件时的字符编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的csv文件包含特殊字符,例如'æ','å'等。当我阅读和打印文件时,文件中的特殊字符被转换为 。
我尝试将页面编码设置为UTF-8和ISO 8859-1。
My csv file contains special characters like 'æ', 'å' etc. When I read and print the file, special characters in the file gets converted into '�'. I tried setting page encodig to UTF-8 and ISO 8859-1. But none of these helped.
可以smb建议解决方案吗?
Could smb advice a solution?
推荐答案
p>我想你必须检测和更改原始编码为folows(如果你使用php):
I think you have to detect and change the original encoding as folows (if you are using php):
<?php
header( "Content-Type: text/html; charset=utf-8");
$csvContent = file_get_contents( $fileName );
$encoding = mb_detect_encoding( $csvContent,
array("UTF-8","UTF-32","UTF-32BE","UTF-32LE","UTF-16","UTF-16BE","UTF-16LE"),
TRUE );
if( $fileEncoding !== "UTF-8" ) {
$csvContent = mb_convert_encoding($csvContent, "UTF-8", $fileEncoding );
}
foreach( explode( PHP_EOL, $csvContent ) as $item ) {
var_dump($item );
}
?>
这篇关于读取csv文件时的字符编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文