The following is an example of php reading and generating a unicodecsv file. I hope this example will help you. the following is an example of how php reads and generates unicode csv files. I hope this example will help you.
Script ec (2); script
======== First introduce 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.
======================================
Read unicode csv files
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 );
}
Generate unicode csv (this PHP file must have no BOM UTF-8 encoding file)
? View Code PHP
$ Content = iconv ("UTF-8", "UTF-16LE", $ content );
$ Content = "\ xFF \ xFE". $ content; // Add BOM
Header ("Content-type: text/csv; charset = UTF-16LE ");
Header ("Content-Disposition: attachment; filename=test.csv ");
Next, we will introduce an operation class that operates ANSI encoding separated ",".
// Unicode BOM is U + FEFF, but after encoded, it will look like this.
Define ('utf32 _ BIG_ENDIAN_BOM ', chr (0x00). chr (0x00). chr (0xFE). chr (0xFF ));
Define ('utf32 _ LITTLE_ENDIAN_BOM ', chr (0xFF). chr (0xFE). chr (0x00). chr (0x00 ));
Define ('utf16 _ BIG_ENDIAN_BOM ', chr (0xFE). chr (0xFF ));
Define ('utf16 _ LITTLE_ENDIAN_BOM ', chr (0xFF). chr (0xFE ));
Define ('utf8 _ BOM ', chr (0xEF). chr (0xBB). chr (0xBF ));
Function detect_utf_encoding ($ filename ){
$ Text = file_get_contents ($ filename );
$ First2 = substr ($ text, 0, 2 );
$ First3 = substr ($ text, 0, 3 );
$ First4 = substr ($ text, 0, 3 );
If ($ first3 = UTF8_BOM) return 'utf-8 ';
Elseif ($ first4 = UTF32_BIG_ENDIAN_BOM) return 'utf-32BE ';
Elseif ($ first4 = UTF32_LITTLE_ENDIAN_BOM) return 'utf-32le ';
Elseif ($ first2 = UTF16_BIG_ENDIAN_BOM) return 'utf-16BE ';
Elseif ($ first2 = UTF16_LITTLE_ENDIAN_BOM) return 'utf-16le ';
}
?>