I am using a script to convert a Magento exported CSV file into a tab delimited TXT file. For some reason however several columns are between quotes. Is there a way to fully remove quotes?
My script:
<?php
$row = 1;
if (($handle = fopen("/var/www/html/var/export/export_orders.csv", "r")) && $myfile = fopen("/var/www/html/var/export/export_orders.txt", "w")) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
fputcsv($myfile, $data, "t");
}
fclose($handle);
fclose($myfile);
}
?>
my output (txt):
100009407 "2018-03-07 02:16:39" processing PayPal freeshipping_freeshipping 6007.0000 0.0000 0.0000 0.0000 6007.0000 6007.0000 6007.0000 0 0 34.0000 2004 "customer name" xxxx "xxxx" [email protected] "xxxx" "address1" 76120 xxxx "xxxx" processing LP012 8855708513503 99.0000 99.0000 2.0000 2.0000 0.0000 0.0000 0.0000 0.0000 0.0000 198.0000 "Home delivery - Nationwide" "10:00 " " 17:00" "2018-03-08 00:00:00"
Why is certain data being quoted? Like I said, I would like to have no quotes at all. Some expert advice would be greatly appreciated, thank you
2
Answers
PHP will put
"
around anything with a space when usingfputcsv
it’s the normal expected behaviour.So this
Has a space, as does
And
"customer name"
etc.
For the most part unless something else is wrong, this should be fine, as PHP can read it fine, Excel will do just fine.
Now if you want to get rid of some of the spaces such as
10:00
then you can use trim and array mapUPDATE
You can use implode or you can put a null byte
" "
orchr(0)
in for the enclosure. You would think putting''
empty in there would work but it doesn’t. But don’t say I told you to do it … lol … it’s a bit of a hack.You can test it with this little piece of code
Outputs:
Also ignore my use of the dynamic stream wrappers….
Though, I don’t know why you do all this, because csv-format is widely used, but if you don’t want any limitations of this format, you can create a string of a required format with just
implod
ing array values andfwrite
it to file: