2

I am working on a project that exports a simple CSV. So far, my code works and generates the CSV as long as I use English characters.

Some column names will be in French and would like to be able to encode that.

Here's what I have:

<?php 
    // open the file "demosaved.csv" for writing
    $file = fopen('myfrench-export.csv', 'w');
    // save the column headers
    fputcsv($file, array('Question', 'Réponse 1/4', 'Réponse 2/4', 'Réponse 3/4', 'Réponse 4/4', 'Total completé', 'Total non-completé', 'Total cumulatif'));

    // Sample data. This can be fetched from mysql too
    $data = array(
        array('Data 11', 'Data 12', 'Data 13', 'Data 14', 'Data 15'),
        array('Data 21', 'Data 22', 'Data 23', 'Data 24', 'Data 25'),
        array('Data 31', 'Data 32', 'Data 33', 'Data 34', 'Data 35'),
        array('Data 41', 'Data 42', 'Data 43', 'Data 44', 'Data 45'),
        array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55'),
        array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55'),
        array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55'),
        array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55'),
    );

    // save each row of the data
    foreach ($data as $row)
    {
        fputcsv($file, $row);
    }

    // Close the file
    fclose($file); ?>
5
  • What is causing the French text to be encoded as "Réponse 3/4" instead of "Réponse 3/4" , and how to fix it? Commented Jan 11, 2017 at 19:25
  • What OS do you code on? And what OS does your code run on? Commented Jan 11, 2017 at 20:30
  • I code on Windows 10, code runs locally on Xampp and remotely on Azure on a Wordpress App. Does it matter though? Commented Jan 11, 2017 at 23:17
  • 1
    using mb_convert_encoding( $string, 'HTML-ENTITIES','utf-8' ); worked for me; whereas utf8_decode wasn't doing the trick. Commented Dec 9, 2020 at 2:39
  • This is in fact not a PHP problem, but an Excel one: a file that is output correctly as UTF-8 needs an additional hint to open in the correct mode in Excel. See linked duplicates. Commented May 20, 2024 at 11:43

4 Answers 4

2

I had a similar issue and tried to use utf8_decode, and for the most part it worked, but some special characters still weren't being displayed properly.

After that I tried writing UTF-8 BOM at the start and that fixed everything.

    $file = fopen('php://output', 'w');
    // Write UTF-8 BOM to ensure proper encoding detection
    fwrite($file, "\xEF\xBB\xBF");
    // Start writing to the CSV
    fputcsv($file, $columns);
Sign up to request clarification or add additional context in comments.

2 Comments

For all I know, the scope of this question was for French Characters which was amply sufficient. Any specific characters in French that weren't covered?
No, the French characters were all fine, but the typographic apostrophe(’) was being displayed incorrectly along with some other symbols. I appreciate the simplicity, the minimal code intrusion, and better performance of this solution. Still, colleagues should take heed whether a Byte Order Mark is suitable in their implementation.
0

After some checking around, I came across this answer and I adjusted this line by applying utf8_decode and that worked.

Basically I replaced

fputcsv($file, array('Question', 'Réponse 1/4', 'Réponse 2/4', 'Réponse 3/4', 'Réponse 4/4', 'Total completé', 'Total non-completé', 'Total cumulatif'));

With

$array = array_map("utf8_decode", array('Question', 'Réponse 1/4', 'Réponse 2/4', 'Réponse 3/4', 'Réponse 4/4', 'Total completé', 'Total non-completé', 'Total cumulatif'));

And that worked!

6 Comments

Not useful, what about the large data set ??
Map accordingly
I was thinking that this code is useful but finally it was not. Unfortunately I cannot undo my vote.
utf8_decode is not a magical fix for all UTF-8 issues. It will re-encode the string into ISO 8859-1, which is mostly compatible with Excel's default of Windows-1252, and has some common non-ASCII characters, but the vast majority of Unicode characters will simply be substituted with "?". It will not work, for instance, to encode a Euro sign (€), since that symbol wasn't invented yet when ISO 8859-1 was standardised.
@IMSoP If you read the actual question, the encoding needed was for French Characters. The provided solution was sufficient for that need
|
0

Use bom to allow to display special characters with excel.

This code can help you:

// Open the file "demosaved.csv" for writing
$file = fopen('myfrench-export.csv', 'w');

// Mandatory: Use bom to allow to display special characters with excel
fwrite($file, $bom = chr(hexdec('EF')).chr(hexdec('BB')).chr(hexdec('BF')));

// Save the column headers
fputcsv($file, array('Question', 'Réponse 1/4', 'Réponse 2/4', 'Réponse 3/4', 'Réponse 4/4', 'Total completé', 'Total non-completé', 'Total cumulatif'));

// Sample data. This can be fetched from mysql too
$data = array(
    array('Data 11', 'Data 12', 'Data 13', 'Data 14', 'Data 15'),
    array('Data 21', 'Data 22', 'Data 23', 'Data 24', 'Data 25'),
    array('Data 31', 'Data 32', 'Data 33', 'Data 34', 'Data 35'),
    array('Data 41', 'Data 42', 'Data 43', 'Data 44', 'Data 45'),
    array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55'),
    array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55'),
    array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55'),
    array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55'),
);

// Save each row of the data
foreach ($data as $row)
{
    fputcsv($file, $row);
}

// Close the file
fclose($file);

Comments

0

This one worked for me: iconv("UTF-8", "ISO-8859-1//TRANSLIT", $content)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.