如何从没有引号的php数组导出制表符分隔的txt文件

时间:2015-04-06 19:23:14

标签: php tsv fputcsv

我已设法使用以下代码保存产品Feed的制表符分隔文件。但是,我提交的Feed要求字段不包含在引号中。有没有办法在字段中没有引号的情况下保存此文件。

$feed[]=array('Item','Description','Category');
$feed[]=array('1-1','Words describing item 1, for example.','Top Category > SubCategory1');
$feed[]=array('1-2','Words describing item 2.','Top Category > SubCategory2');

header('Content-type: text/tab-separated-values');
header("Content-Disposition: attachment;filename=bingproductfeed.txt");
$f  =   fopen('php://output', 'a');
foreach ($feed as $fields) {
    //$fields=str_replace('"','',$fields);
    //$fields=trim($fields,'"');
    fputcsv($f, $fields, "\t");
}

//Outputs:
//Item  Description Category
//1-1   "Words describing item 1, for example." "Top Category > SubCategory1"
//1-2   "Words describing item 2."  "Top Category > SubCategory2"

//I need:
//Item  Description Category
//1-1   Words describing item 1, for example.   Top Category > SubCategory1
//1-2   Words describing item 2.    Top Category > SubCategory2

我已经尝试修剪引号并用空格替换它们,但那里没有运气。有没有办法做到这一点,所以我可以提交这个饲料没有错误?

1 个答案:

答案 0 :(得分:3)

基于PHP manual我会说你可以省略fopen行,直接在页面上回显你的输出。

  

php://输出¶

     

php:// output是一个只写流,允许您以与print和echo相同的方式写入输出缓冲区机制。

这样的事情:

$feed[]=array('Item','Description','Category');
$feed[]=array('1-1','Words describing item 1, for example.','Top Category > SubCategory1');
$feed[]=array('1-2','Words describing item 2.','Top Category > SubCategory2');

header('Content-type: text/tab-separated-values');
header("Content-Disposition: attachment;filename=bingproductfeed.txt");
foreach ($feed as $fields) {
    //$fields=str_replace('"','',$fields);
    //$fields=trim($fields,'"');
    echo implode("\t",$fields);
}