2

I've been at this one for a while. I'm trying to create a .csv file from a php script (that get the data from a database). My problem is that special characters are not displayed correctly.

Weird this is that the special characters that come from the database are shown correctly but he thing is I need to add certain characters to the entries.

For example, the database returns "Humidité" (French for humidity). That shows up correctly in the exported .csv.

But lets say I add a string to it from my php script

$member->humidity . ': 10% à 20%';

In the csv I will see

Humidité: 10% [weird characters] 20%

So the "é" from humidité is ok, but I don't see the "à" I added in PHP.

My headers seem ok, since some special characters are correctly displayed

$data = the whole csv file in a string      

header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header("Content-Disposition: attachment; filename=file.csv");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header("Content-Length: " . strlen($data));
echo "\xEF\xBB\xBF"; // UTF-8 BOM
echo $data;

So my guess is that the problem comes from my Apache server.

When I found this I changed my default_charset = "utf-8" in my php.ini and added AddDefaultCharset UTF-8 in my httpd.conf bu that didn't solve it.

Any ideas? Thanks!

4
  • Does your table in mysql is in UTF 8 format ? Commented Aug 23, 2012 at 16:29
  • @TuhinSubhraDey No they are in "latin1_swedish_ci" Commented Aug 23, 2012 at 16:31
  • try change it in utf 8 general ci then save your data and rest things.. Commented Aug 23, 2012 at 16:32
  • @TuhinSubhraDey Just did. The result is exactly the same. Thanks anyway! Commented Aug 23, 2012 at 16:36

2 Answers 2

4

Probably, your editor isn't set to UTF-8. Check your editor's charset settings. You can also convert your file using an editor such as Notepad++.

Sign up to request clarification or add additional context in comments.

5 Comments

Reason for downvote? The only char which is not printed correctly is the one he inserted statically in the file. Thus, it can most likely only be the editor's charset.
Downvote wasn't from me! I opened the file in Dreamweaver, Excel, and TextEdit (Mac), all 3 show exactely the same thing.
@ComputerArts: If you're using Dreamweaver, check this: tlt.its.psu.edu/suggestions/international/web/tips/…
Update: To explain my thoughts: If your editor uses the wrong charset, the data you type into your PHP files will be encoded wrong. The Client application, however, receives headers that tell him "Hey, interpret this as UTF-8!" and this causes errors if your file itself is not correctly UTF-8 encoded.
Wasn't me that downvoted, but rather than promoting one editor, a more general "many good editors, such as Notepad++" might be more helpful.
0

You need to tell both PHP and MySQL that your data is UTF-8, because they both default to ASCII (or at least they did for me). I did this with:

setlocale(LC_ALL, 'en_US.utf8');
mysql_query("SET NAMES 'utf8'");

Which seems to work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.