One small point that I have come across which has caused me no end of pain ! I have had a look for solutions but haven't noticed anything relevant.
I had a piece of code to extract required fields from an XML file and write them to a CSV file for import.
I was originally using fputcsv for this but I discovered that it has a mind of it's own when deciding what fields to use quote marks and not.
I have now had to go the long way round and use fput and some jiggling to make sure each field is written with quote marks.
Is there any real reason why quote marks are needed ??
For anyone interested, here is how I did it. I used some other code I found online and modified it slightly. I don't guarantee that it will be perfect but it does seem to work. Should have more error checking.
Code:
function sendCSV($data_array, $enclosure, $field_sep, $record_sep)
{
dumbescape(false, $enclosure);
$data_array=array_map('dumbescape',$data_array);
//return
$uploadFile = "output" . (12*1024*1024) . ".csv";
$file = fopen($uploadFile, 'a+');
fputs($file, $enclosure . implode($enclosure . $field_sep . $enclosure, $data_array) . $enclosure . $record_sep);
fclose($file);
}
function dumbescape($in, $enclosure=false)
{
static $enc;
if ($enclosure===false) {
return str_replace($enc, '\\' . $enc, $in);
}
$enc=$enclosure;
}
Then
Code:
sendCSV($my_array, '"', ',' , "\n");
I know it is probably slow (and a lot slower than fputcsv) but this will give correctly formatted CSV for EP 4. Any suggestions on improving this would be gratefully appreciated :-)
If anyone wants more info on how I got the fields out of XML then I can post the rest. Again, it's not professional (because I'm not a professional !) but it works for me.
B. Rgds
John
Bookmarks