MatthewNLane Posted December 3, 2012 Share Posted December 3, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271542-output-database-content-to-csv-with-php-special-characters/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 3, 2012 Share Posted December 3, 2012 http://en.wikipedia.org/wiki/Comma-separated_values#Basic_rules_and_examples Quote Link to comment https://forums.phpfreaks.com/topic/271542-output-database-content-to-csv-with-php-special-characters/#findComment-1397306 Share on other sites More sharing options...
MatthewNLane Posted December 4, 2012 Author Share Posted December 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271542-output-database-content-to-csv-with-php-special-characters/#findComment-1397424 Share on other sites More sharing options...
Christian F. Posted December 4, 2012 Share Posted December 4, 2012 Sounds like fputcsv () is the function you want to use, as it'll escape such things automatically. Quote Link to comment https://forums.phpfreaks.com/topic/271542-output-database-content-to-csv-with-php-special-characters/#findComment-1397429 Share on other sites More sharing options...
MatthewNLane Posted December 4, 2012 Author Share Posted December 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271542-output-database-content-to-csv-with-php-special-characters/#findComment-1397435 Share on other sites More sharing options...
kicken Posted December 4, 2012 Share Posted December 4, 2012 I did take a look at fputcsv, but to be honest was way over my head There is an example of fputcsv over in a previous post of mine. It is a very easy and worthwhile function to use. Makes CSV exports a snap. Quote Link to comment https://forums.phpfreaks.com/topic/271542-output-database-content-to-csv-with-php-special-characters/#findComment-1397437 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.