简体   繁体   中英

PHP fputcsv with UTF-8 Problem

I'm trying to allow my clients view some of the MySQL data in Excel. I have used PHP's fputcsv() function, like:

public function generate() {
   setlocale(LC_ALL, 'ko_KR.UTF8');

   $this->filename = date("YmdHis");
   $create = $this->directory."Report".$this->filename.".csv";

   $f = fopen("$create","w") or die("can't open file");
   fwrite($f, "\xEF\xBB\xBF");

   $i = 1;
   $length = count($this->inputarray[0]);

   fwrite($f, $this->headers."\n");

   // print column titles
   foreach($this->inputarray[0] as $key=>$value) {
    $delimiter = ($i == $length) ? "\n\n" : ",";
    fwrite($f, $key.$delimiter);
    $i++;
   }

   // print actual rows
   foreach($this->inputarray as $row) {
    fputcsv($f, $row);
   }
   fclose($f);
  } 

My clients are Korean, and a good chunk of the MySQL database contains values in utf8_unicode_ci. By using the above function, I successfully generated a CSV file with correctly encoded data that opens fine in Excel on my machine (Win7 in English), but when I opened the file in Excel on the client computer (Win7 in Korean), the characters were broken again. I tried taking the header (\\xEF\\xBB\\xBF) out, and commenting out the setlocale, to no avail.

Can you help me figure this out?

If, as you say, your CSV file has "correctly encoded data" - ie that it contains a valid UTF-8 byte stream, and assuming that the byte stream of the file on your client's site is the same (eg has not been corrupted in transit by a file transfer problem) then it sounds like the issue Excel on the client's machine not correctly interpreting the UTF-8. This might be because it's not supported or that some option needs to be selected when importing to indicate the encoding. As such, you might try producing your file in a different encoding (using mb_convert_encoding or iconv ).

If you get your client to export a CSV containing Korean characters then you'll be able to take a look at that file and determine the encoding that is being produced. You should then try using that encoding.

Try encoding the data as UTF-16LE, and ensure that the file has the appropriate BOM.

Alternatively, send your clients an Excel file rather than a CSV, then the encoding shouldn't be a problem

Try wrapping the text in each fwrite call with utf8_encode .

Then use what is suggested here: http://www.php.net/manual/en/function.fwrite.php#69566

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM