读取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 );
}
?>