How can I create a CSV file with PHP that preserves the Japanese characters? -


below code snippet creating , downloading csv file browser.

$input_array[] = ['注文日時', '受注番号',]; $input_array[] = ['2015-09-30', 'inv-00001',];      /** open raw memory file, no need temp files, careful not run out of memory thought */     $f = fopen('php://memory', 'w');     /** loop through array  */     foreach ($input_array $line) {         /** default php csv handler **/         fputcsv($f, $line, ',');     }     /** rewrind "file" csv lines **/     fseek($f, 0);      /** modify header downloadable csv file **/     header('content-encoding: utf-8');     header('content-type: application/csv; charset=utf-8');     header('content-disposition: attachement; filename="my_csv_file.csv";');      /** send file browser download */     fpassthru($f);      die(); 

when open created/downloaded csv file, japanese characters become weird characters. not yet correct in code snippet? how can preserve japanese characters when creating csv file?

just add byte order mark simple echo directly before writing first line if want force program interpret file utf-8 encoding:

$input_array[] = ['注文日時', '受注番号',]; $input_array[] = ['2015-09-30', 'inv-00001',]; echo "\xef\xbb\xbf";/// byte order mark here!!!!      /** open raw memory file, no need temp files, careful not run out of memory thought */     $f = fopen('php://memory', 'w');     /** loop through array  */     foreach ($input_array $line) {         /** default php csv handler **/         fputcsv($f, $line, ',');     }     /** rewrind "file" csv lines **/     fseek($f, 0);      /** modify header downloadable csv file **/     header('content-encoding: utf-8');     header('content-type: application/csv; charset=utf-8');     header('content-disposition: attachement; filename="my_csv_file.csv";');      /** send file browser download */     fpassthru($f);      die(); 

alternatively select unicode character set when import excel or open calc, otherwise opening directly notepad/textedit/etc there no problem importing


Comments

Popular posts from this blog

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

android - How to create dynamically Fragment pager adapter -

1111. appearing after print sequence - php -