0

Im building a hand made export system for educational purposes in my new website and im having problem with the encoding of the csv in the end. (greek characters)

This is the problem

The code of the script:

if(isset($_POST['export'])){
    header("Content-Encoding: UTF-8");
    header("Content-type: text/csv; charset=utf8");
    header("Content-Disposition: attachment; filename=oramatismos.csv");

    $export_query = $connect->prepare("SELECT * FROM oramatismos");
    $export_query->execute();

    fputcsv($file, explode(',',$titles));

    while($list = $export_query->fetch(PDO::FETCH_ASSOC)){
        fputcsv($file,$list,';',' ');
    }

    fclose($file);
    exit();
}

The database table columns are set to utf8_unicode_ci.

The website is build with PDO and in the connection the code is as follows:

try{
    $connect = new PDO("mysql:host=".HOST.";dbname=".DB.";charset=utf8",
               DB_USER,DB_PASS);
} catch(Exception $e){
    echo $e->getMessage();
}
trincot
  • 317,000
  • 35
  • 244
  • 286
  • Any link on where i can read about chr(0xEF).chr(0xBB).chr(0xBF)? – user3778909 Dec 23 '15 at 22:22
  • That's called a "byte order mark" or just a BOM – Chris Haas Dec 23 '15 at 22:23
  • https://en.wikipedia.org/wiki/Byte_order_mark – trincot Dec 23 '15 at 22:24
  • You should be warned, however, that this is usually only a one-time thing. If you kick out a CSV with a BOM, open it in Excel and Save (not Save As) you'll most likely lose all of your Unicode data (along with the BOM) and Excel won't even warn you. – Chris Haas Dec 23 '15 at 22:27
  • I see then its not a solution for the above :/ – user3778909 Dec 23 '15 at 22:29
  • CSV is the most basic of formats without an agreed upon standard. Someone made one but it was a couple of decades too late. People can argue all they want but ultimately you'd need Microsoft to change their implementation (which is what most people really want) and you can guess what Microsoft will say... – Chris Haas Dec 23 '15 at 22:37

0 Answers0