title: PHP export CSV UTF-8 with BOM doesn't work
body: <p>I have been stuck for days on exporting UTF-8 CSV with chinese characters that shows garbled text on Windows Excel. I am using PHP and have already added the BOM byte mark and tried encoding but no luck at all.</p>
  
  <p>They open fine on Notepad++, Google Spreadsheet and even on Mac Numbers. But not on Excel which is a requirement by the client. When opening with Notepad++ the encoding is shown as UTF-8. If I change it to UTF-8 manually and save, the file opens fine on Excel.</p>
  
  <p>It seems as though the BOM byte mark doesn't get saved in the output as Notepad++ always detect it as UTF-8 without BOM.</p>
  
  <p>Also, the CSV is not saved on server. Data is retrieved from DB and then exported directly out.</p>
  
  <p>Here are my codes:</p>
  
  <pre><code>// Setup headers
  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  header('Content-Description: File Transfer');
  header("Content-type: text/csv");
  header("Content-disposition: filename=".$filename.".csv");
  header("Pragma: no-cache");
  
  // First Method
  $fp = fopen('php://output', 'w');
  // Add BOM to fix UTF-8 in Excel, but doesn't work
  fputs($fp, chr(0xEF) . chr(0xBB) . chr(0xBF) );
  
  if ($fp) {
  
      fputcsv($fp, array("Header"), ",");
      fputcsv($fp, array($string_with_chinese_chars), ",");
  }
  
  fclose($fp);
  exit();
  
  // Second Method
  $csv = "";
  $sep = ",";
  $newline = "\n"; // Also tried with PHP_EOL
  
  $csv .= "Header";
  $csv .= $newline;
  $csv .= $string_with_chinese_chars;
  $csv .= $newline;
  
  // Tried all the below ways but doesn't work.
  // Method 2.1
  print chr(255) . chr(254) . mb_convert_encoding($csv, 'UTF-16LE', 'UTF-8');
  
  // Method 2.2
  print chr(239) . chr(187) . chr(191) . $csv;
  
  // Method 2.3
  print chr(0xEF).chr(0xBB).chr(0xBF);
  print $newline;
  print $csv;
  </code></pre>