下文给各位整理了一篇关于php 读取 和 生成 unicode csv 文件的例子,希望这个例子可以给各位带来帮助的哦.
=======先介绍下bom==============
bytes encoding form
ef bb bf utf-8
ff fe utf-16 aka ucs-2, little endian
fe ff utf-16 aka ucs-2, big endian
00 00 ff fe utf-32 aka ucs-4, little endian.
00 00 fe ff utf-32 aka ucs-4, big-endian.
=======================
读取 unicode csv 文件
function fopen_utf8($filename){
$encoding='';
$handle = fopen($filename, 'r');
$bom = fread($handle, 2);
// fclose($handle);
rewind($handle);
if($bom === chr(0xff).chr(0xfe) || $bom === chr(0xfe).chr(0xff)){
// utf16 byte order mark present
$encoding = 'utf-16';
} else {
$file_sample = fread($handle, 1000) + 'e'; //read first 1000 bytes
// + e is a workaround for mb_string bug
rewind($handle);
$encoding = mb_detect_encoding()($file_sample , 'utf-8, utf-7, ascii, euc-jp,sjis, eucjp-win, sjis-win, jis, iso-2022-jp');
}
if ($encoding){
stream_filter_append($handle, 'convert.iconv.'.$encoding.'/utf-8');
}
return ($handle);
}
生成 unicode csv (此php文件一定要是无bom的utf-8编码文件)
?view code php
$content=iconv(utf-8,utf-16le,$content);
$content = \xff\xfe.$content; //添加bom
header(content-type: text/csv;charset=utf-16le) ;
header(content-disposition: attachment; filename=test.csv);
再介绍一个 操作 ansi 编码 以 , 隔开的 操作类