0

I'm creating csv file with UTF-8 encoding using PHP code, when i use UTF-8 encode it prints special characters not correctly,

Code :

$fp = fopen('php://temp', 'w');
fprintf($fp, chr(0xEF).chr(0xBB).chr(0xBF)); // encode to UTF8
fputcsv($fp,$row,';',chr(0));


  rewind($fp);
                 Yii::app()->user->setState('export',stream_get_contents($fp));
                 fclose($fp);
                 Yii::app()->request->sendFile($name.'.csv',Yii::app()->user->getState('export'));
                 Yii::app()->user->clearState('export');

when i use fprintf($fp, chr(0xEF).chr(0xBB).chr(0xBF)); this the file is in UTF-8 encode but special characters are wrongly displayed Honor� de Balzac 45100 Orl�ans.

Please anyone provide me an idea to resolve this case

US-1234
  • 1,519
  • 4
  • 24
  • 61
  • Ist your script utf8-encoded? Just an idea. – Oliver Feb 22 '17 at 10:10
  • How are you trying to display the generated file? – Mark Baker Feb 22 '17 at 10:10
  • @MarkBaker yess – US-1234 Feb 22 '17 at 10:14
  • Interesting answer to my question... I'm not familiar with a file viewer called yess; does it support UTF-8? – Mark Baker Feb 22 '17 at 10:40
  • Despite your comment, printing `0xEFBBBF` does nothing to set or change the encoding of your data, it's merely an *indication* the spreadsheet software can choose to use. If `$row` is e.g. `Windows-1252` you'll end up with `Windows-1252` incorrectly advertised to be UTF-8. – Álvaro González Feb 22 '17 at 11:01
  • @MarkBaker i am generating csv file, CSV file should be UTf-8 not with UTF8-BOM – US-1234 Feb 22 '17 at 11:30
  • Then ensure that all the data you write to the csv is utf-8 before writing it; and don't write the BOM... simply writing a BOM to a file doesn't do anything magical for subsequent writes – Mark Baker Feb 22 '17 at 11:31
  • but my client wants file to in UTF8 encode – US-1234 Feb 22 '17 at 11:45
  • In fact, it really doesn't matter how badly your client and you want it, or how strong you wish it: your byte stream in some arbitrary/unknown encoding is not going to become UTF-8 for the sake of it. You can try adding more "file has to be UTF-8" comments but the result will be the same. /// Since this is getting nowhere, I'm closing with a hopefully useful duplicate. – Álvaro González Feb 22 '17 at 13:13

0 Answers0