代码之家  ›  专栏  ›  技术社区  ›  Austin Hyde

将PHP fputcsv()生成的CSV值包装为“”

  •  10
  • Austin Hyde  · 技术社区  · 15 年前

    因此,我的代码使用PHP的内置 fputcsv 功能。

    对于分隔符,我使用 ',' (逗号)。
    对于外壳,我使用 '"'

    但是,当我尝试

    fputcsv($file,array('a','b',"long string, with commas",NULL,''),',','"');
    

    a,b,"long string, with commas",,
    

    但我希望它能输出

    "a","b","long string, with commas","",""
    

    fputcsv文件 ?

    5 回复  |  直到 15 年前
        1
  •  9
  •   Thomas O    15 年前

    对于CSV文件,这通常不是问题。

    如果值不明确,fputcsv会在其周围加引号。例如,

    a,b,"long string, with commas",,
    

    不是模棱两可,但是,

    a,b,long string, with commas,,
    

    在大多数(read:all)情况下,CSV读取器会将其解释为具有5个以上的字段。

    CSV解析器将接受字符串文本,即使它们周围没有引号。

    如果您希望在值周围加引号,下面的代码段可以这样做。它不会在字符串中转义引号-这个练习留给读者:

    $row = '"' . implode('", "', $rowitems) . '"';
    

    你应该把它放在所有行的循环中。

        2
  •  8
  •   periklis    14 年前

    为了解决这个问题,我插入了一些带有空格的伪字符串#@@#,然后删除它们。下面是一个示例实现:

    //$exported is our array of data to export
    $filename = 'myfile.csv';
    $fp = fopen($filename, 'w');
    foreach ($exported as $line => $row) {
        if ($line > 0) {
            foreach ($row as $key => $value) {
                    $row[$key] = $value."#@ @#";
            }
        }
        fputcsv($fp, $row);
    }
    
    fclose($fp);
    $contents = file_get_contents($filename);
    $contents = str_replace("#@ @#", "", $contents);
    file_put_contents($filename, $contents);
    

        3
  •  2
  •   Austin Hyde    15 年前

    我想解决办法是这样的,

    $order_header_arr = array("Item1", "Item2","This is Item3");
    fputcsv($fp, $order_header_arr,',',' ');
    

    记得 " " fputcsv的第三个参数之间的[Space]

        4
  •  0
  •   methodin    15 年前

    有什么原因不能str_replace(',',',',“”,',$output)?您还必须查看最后一个或第一个字符是否是逗号,如果是,请将逗号替换为“”

        5
  •  0
  •   roicarl    9 年前

    fputcsv不会将所有数组变量括在引号中。使用不带引号的数字数组值可能是正确的,但当标签或地址程序遇到数字定义的美国邮政编码时会出现问题,因为它在打印时会去掉前导零。因此,05123-0019变为5123-19。

    为了将所有值括起来,不管它们是否存在,我用fgetsrc读取了输入文件,并用fwrite编写了一个正确的版本。fgetsrc将记录读入数组变量。因为fwrite写变量,所以必须将数组变量串起来,用引号括起来,并用逗号分隔数组变量。然后添加记录分隔符。

    <?php
    // fgetcsv - read array with fgetcsv and string into output variable 
    // then write with fwrite
    // $ar is the array populated from fgetcsv and $arc is the variable strung 
    // with array variables enclosed in quotes and written using fwrite.
    $file_in = fopen("reinOCp.csv","r") or die("Unable to open input file 
    reinOCp.csv!"); 
    $file_out = fopen("printLABEL.csv", "w") or die("Unable to open output file 
    prtLABEL!");
    while (!feof($file_in)) {  //loop through each record of the input file
        $ar=fgetcsv($file_in); //read the record into array $ar   
        if (is_array($ar)){ //this loop will string all the array values plus 
    // the end of record into variable $arc and then write the variable 
            $arc = ""; //clear variable $arc  
            foreach ($ar as $value) {      
                $arc .= '"' . $value . '",'; // add the array values, enclose in 
    // quotes with comma separator and store in variable $arc
            }   
            $arc .= "\n"; //add end of record to variable $arc
            fwrite($file_out, $arc) or die ("ERROR: Cannot write the file"); 
    //write the record using variable $arc
        }
    }
    echo "end of job";
    fclose($file_in);
    fclose($file_out);
    ?>
    
    推荐文章