Exporting to csv – ’ replacing ‘

Came across an interesting and frustrating issue last week. A client was exporting selected data from a WordPress database (actually using this plugin), and the user was seeing odd characters in the csv output. The most obvious was the replacement of single apostrophes: ‘ with this: ’.

Clearly an encoding problem but I couldn’t immediately see why. Pages displaying the data were fine, web pages and the database were all set to use utf-8. I finally solved the problem by forcing the encoding of the csv file via:

$text = mb_convert_encoding($text,'windows-1252');

Hope this helps someone else…

2 responses to “Exporting to csv – ’ replacing ‘”

  1. Tom Addis says:

    Hi Simon,

    Thanks for posting about this problem – I’ve been searching but can’t find any solution to my .csv encoding troubles.

    Would you be able to let me know where you added this code – child theme functions file?

    Any advice much appreciated!

    Thanks,

    Tom

  2. Simon says:

    Hi Tom
    I think in this case I edited the plugin that was creating the export directly to force the encoding.
    Regards
    Simon

Useful? Interesting? Leave me a comment

I've yet to find a way of allowing code snippets to be pasted into Wordpress comments - so if you're trying to do this you'd be better off using the contact form.