Jump to content

Output Database Content To Csv With Php - Special Characters!


Recommended Posts

Hi hopefully someone can help, i'm having a slight panic on a project I'm working on. I've got 90% of this all done but am having a few concerns regarding some special characters.


Heres' the situation. I have a database table i need to export to a CSV (so the client can then import into their own system) I've got this working a dream, downloads the file and all that perfectly.


Now i have a few fields in this table where we're going to get special characters (double quotes, single quotes etc) just the kind of thing that will bust a CSV from importing (or opening in excel/similar properly).


Basically what i could do with knowing is should i just clean the strings of the bad characters? possibly loosing some of the meaning of the data (for instance one field is a message, and this is likely to contain all sorts of characters), or is there something else i can do that will retain the data - i'm thinking the brute force clean could be fine, but would like to know my options.


Also could do with a hand with regards to cleaning the string, I've had a look at preg_replace, example here:


$fnameClean = preg_replace("/'/", "", $fnameDirty);


Really hope this is a simple one guys, thanks in advance.

Link to comment
Share on other sites

Thanks for the response, so if i am reading it correctly I basically have to use preg_replace to sort out the double quotes within the content (message) as all other characters are fine to be within a field?


So if the field had the following in it:


This is the "car of the year"


In my CSV field it would read like this:


"This is the ""car of the year"""


With every double quote mark doubled again, much like adding slashes.

Link to comment
Share on other sites

I did take a look at fputcsv, but to be honest was way over my head, i've also got to structure the output CSV to match the database it will import into.


I've managed to get the CSV output working now, and have tested with special chars and seems to be working fine. Really appreciate the help chaps.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.