删除生成文件中的引号

remove quotes in generated file

我正在使用脚本将 Magento 导出的 CSV 文件转换为制表符分隔的 TXT 文件。但是出于某种原因,引号之间有几列。有没有办法完全删除引号?

我的脚本:

    <?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);
    }
    ?>

我的输出(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"  xxxx4@gmail.com "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"   

为什么引用某些数据?就像我说的,我希望完全没有引号。非常感谢专家的建议,谢谢

PHP 会在使用 fputcsv 时将 " 放在带有 space 的任何东西周围,这是正常的预期行为。

所以这个

"10:00 "

有 space,

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

"customer name"

等等

在大多数情况下,除非有其他问题,否则应该没问题,因为 PHP 可以很好地阅读它,Excel 就可以了。

现在如果你想摆脱一些 spaces 比如 10:00 那么你可以使用 trim 和数组映射

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

更新

您可以使用 implode,也可以将空字节 "[=19=]"chr(0) 放入外壳中。你会认为把 '' 空在那里会起作用,但事实并非如此。但是不要说我告诉你这样做......大声笑......这有点骇人听闻。

你可以用这段代码测试一下

$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));

输出:

the fox\tjumpled\tover the rox

也请忽略我对动态流包装器的使用....

不过,我不知道你为什么要这样做,因为csv-format被广泛使用,但如果你不希望这种格式有任何限制,你可以创建一个所需格式的字符串仅 imploding 数组值并将其 fwrite 归档:

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);