Encoding MySQL text fields into UTF-8 text files - problems with special characters

Posted by Matt Andrews on Stack Overflow See other posts from Stack Overflow or by Matt Andrews
Published on 2010-04-20T13:27:17Z Indexed on 2010/04/20 13:33 UTC
Read the original article Hit count: 372

Filed under:
|
|
|
|

I'm writing a php script to export MySQL database rows into a .txt file formatted for Adobe InDesign's internal markup.

Exports work, but when I encounter special characters like é or umlauts, I get weird symbols (eg Chloë Hanslip instead of Chloë Hanslip). Rather than run a search and replace for every possible weird character, I need a better method.

I've checked that when the text hits the database, it's saved properly - in the database I see the special characters. My export code basically runs some regular expressions to put in the InDesign code tags, and I'm left with the weird symbols. If I just output the text to the browser (rather than prompt for a text file download), it displays properly. When I save the file I use this code:

header("Content-disposition: attachment; filename=test.txt");

header("Content-Type: text/plain; charset=utf-8");

I've tried various combinations of utf8_encode() and iconv() to no avail. Can anybody point me in the right direction here?

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql