带引号的csv输出中的PHP问题

时间:2014-07-11 17:01:07

标签: php csv

你好我有一个csv文件,当我在excel中打开它看起来很干净,在每行的开头或结尾都没有引号,在wordpad中看起来像这样:

10038,"Service, Inc.",loten,4u@att.net,9951,xxx,6321
10041,RoadsideInc.,Kgel,gel1980@gmail.com,1101,xxx,7967
10042,RangLLC,Resgers,resranger85@yahoo.com,2073,4611,xxx
10050,5-Dg,5-dg,pik37@gmail.com,3-4874,2838389,xxx
10053,SandserviceInc.,Service Center Inc.,br@sands.com,6-5556,861398,xx
10055,CenStammddy ,Mman,jooan@an.com,6-1449,xx,36-3755
10060,"BetsyRES, C",ElHies,hines@ol.com, 8-0306,237602,xx
10061,Hampnemes LLC,DBERG@gmail.com,83-7999,xxx,-74-1043,xx
10066,CarReal LLC,CarPRea LLC,selby@c.r.com,8889,456440,xx

当我通过下面的脚本在PHP中读取该文件时,我得到了我想要的有效结果,如下所示:

Array
(
    [0] => 10038|Service, Inc.|loten|4u@att.net|9951|xxx
    [1] => 10041|RoadsideInc.|Kgel|gel1980@gmail.com|1101|xxx
    [2] => 10042|RangLLC|Resgers|resranger85@yahoo.com|2073|4611
    [3] => 10050|5-Dg|5-dg|pik37@gmail.com|3-4874|2838389
    [4] => 10053|SandserviceInc.|Service Center Inc.|br@sands.com|6-5556|861398
    [5] => 10055|CenStammddy |Mman|jooan@an.com|6-1449|xx
    [6] => 10060|BetsyRES, C|ElHies|hines@ol.com| 8-0306|237602
    [7] => 10061|Hampnemes LLC|DBERG@gmail.com|83-7999|xxx|-74-1043
    [8] => 10066|CarReal LLC|CarPRea LLC|selby@c.r.com|8889|456440
)

当我读取文件并保存数组时,我使用下面的PHP代码输入一个新的csv文件作为输出我得到一个输出csv文件,在excel看起来没问题,但在wordpad中打开我看到很多行开始和结束引用如下:

"10038|Service, Inc.|loten|4u@att.net|9951|xxx"
10041|RoadsideInc.|Kgel|gel1980@gmail.com|1101|xxx
10042|RangLLC|Resgers|resranger85@yahoo.com|2073|4611
10050|5-Dg|5-dg|pik37@gmail.com|3-4874|2838389
"10053|SandserviceInc.|Service Center Inc.|br@sands.com|6-5556|861398"
"10055|CenStammddy |Mman|jooan@an.com|6-1449|xx"
"10060|BetsyRES, C|ElHies|hines@ol.com| 8-0306|237602"
"10061|Hampnemes LLC|DBERG@gmail.com|83-7999|xxx|-74-1043"
"10066|CarReal LLC|CarPRea LLC|selby@c.r.com|8889|456440"

我保存csv输出文件的方式有问题吗?

<?php

function createCSV($rows){
    $csv_filename = "output.csv";
    $fp = fopen($csv_filename, "w");
    foreach ($rows as $field) {
      fputcsv($fp, array($field));
    }
    fclose($fp); 
    echo "csv created";
}
$result = array();
        $row = 1;
        if (($handle = fopen(__DIR__.'/stack.csv', "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                $data_row = array(
                    $data[0],
                    $data[1],
                    $data[2],
                    $data[3],
                    $data[4],
                    $data[5]
                 );                            
                $result[] = implode("|", $data_row);
                $row++;
            }
        }
        echo "<pre>";
        print_r($result);
        echo "</pre>";
        fclose($handle);
        createCSV($result);

csv输入和输出文件如下:

input csv

output csv

2 个答案:

答案 0 :(得分:1)

嗨尝试保存这样的数据

fputcsv ( resource $handle , array $fields [, string $delimiter = "," [, string $enclosure = '"' ]] )

或者你的情况

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                $data_row = array(
                    $data[0],
                    $data[1],
                    $data[2],
                    $data[3],
                    $data[4],
                    $data[5]
                 );                            
               // $result[] = implode("|", $data_row); -- change this line to
                 $result[] =  $data
                $row++;
            }

更改此功能以包含您的删除者,并且不要将$ field(这是一个数组)包装在另一个数组中

function createCSV($rows){
    $csv_filename = "output.csv";
    $fp = fopen($csv_filename, "w");
    foreach ($rows as $field) {
      fputcsv($fp, $field, "|");
    }
    fclose($fp); 
    echo "csv created";
}

这里更新是一种更有效的方法(你不需要构建结果数组并循环2次)

        $csv_filename = "output.csv";
        $fp = fopen($csv_filename, "w");
        if (($handle = fopen(__DIR__.'/stack.csv', "r")) !== FALSE && $fp) {
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                $data_row = array(
                        $data[0],
                        $data[1],
                        $data[2],
                        $data[3],
                        $data[4],
                        $data[5]
                );

                echo "<pre>";
                print_r($data_row);
                echo "</pre>";
                fputcsv($fp, $data_row, "|");
            }
        }

        fclose($handle);
        fclose($fp);
        echo "csv created"

答案 1 :(得分:0)

您需要更改用于在磁盘上写入的函数调用,如下所示:

fputcsv($fp, array($field),"|","");

这是因为最后一个参数是定义线条的外壳字符串的参数,如php manual中所述:

int fputcsv ( resource $handle , array $fields [, string $delimiter = "," [, string $enclosure = '"' ]] )

默认值是您要删除的引用