skip to Main Content

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


  1. PHP will put " around anything with a space when using fputcsv it’s the normal expected behaviour.

    So this

    "10:00 "
    

    Has a space, as does

    "2018-03-07 02:16:39"
    

    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 map

     $data = array_map('trim', $data);
    

    UPDATE

    You can use implode or you can put a null byte "" or chr(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

    $f = fopen('php://temp', "w+");
    
    $a = ["the fox", "jumpled", "over the rox"];
    
    fputcsv($f, $a, "t", chr(0));
    
    rewind($f);
    
    echo str_replace("t", 't', fgets($f));
    

    Outputs:

    the foxtjumpledtover the rox
    

    Also ignore my use of the dynamic stream wrappers….

    Login or Signup to reply.
  2. 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 imploding array values and fwrite it to file:

    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        $row++;
        fwrite($myfile, implode("t", $data) . "n");
        // or with PHP_EOL as line end
        // fwrite($myfile, implode("t", $data) . PHP_EOL);
    }
    fclose($handle);
    fclose($myfile);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search