0

我正在尝试创建一个动态 csv 文件以导出到我网站上的供应商网站。他们希望所有值都用双引号括起来。我正在尝试创建该行,然后将其提供给 fputcsv() 但是当我在 notepad2 中查看它时它会出现大量双引号,而在 Excel 中查看它时它会作为 1 个条目出现。这是我的价值观:

$line = '"'.$row->petID.'","'.$row->customID.'","'.$row->petName.'","'.trim($row->breedName1).'","'.trim($row->breedName2).'","'.$df->setGender($row->gender).'","'.$df->setSize($row->size).'","'.$df->setAge($row->age).'","'.$description.'","'.$row->pType.'","'.$df->setStatus($row->status).'","","'.$row->isAltered.'","'.$ND.'","'.$NC.'","'.$NK.'","'.$df->isHousetrained($row->housebroken).'","'.$df->isDeclawed($row->declaw).'","'.$df->isSpecialNeeds($row->isSpecial).'","'.$df->isMix($row->breed2).'","'.$PhotoURL1.'","'.$PhotoURL2.'","'.$PhotoURL3.'"';

使用文本编辑器输出如下:"""13651"","""",""jack"",""Chihuahua"",""Pomeranian"",""M"​​",""S "",""Baby"",""Jack 和 Jose 出生于一只可爱的 7 磅吉娃娃/博美犬混血儿,他们作为流浪狗被带到收容所。他们将在 8 月中旬为新家断奶。尽管父亲不详, 幼犬很可能仍然很小。请访问我们的网站 www.dogswithoutborders.org 以获取 Jack 或 Jose 的在线收养申请,并了解有关我们收养博览会的更多信息。"",""Dog"",""A" ","""",""0"","""","""","""","""","""","""",""1"","" http://photos.petfinder.com/photos/US/CA/CA1087/20460152/CA1087。 20460152-1-x.jpg "","""","""""

当我删除手动添加的“(s)”时,我一开始只得到一个,最后一个。有什么想法吗?

4

2 回答 2

5

使用内置的 CSV 功能!

<?php
    $fp = fopen('file.csv', 'w');
    fputcsv($fp, array('item1','items'));
    fclose($fp);
?>

参考

于 2013-02-16T20:54:18.130 回答
-1

您可以使用这个 PHP 函数,而不是自己做:

fputcsv

文档在这里: http: //php.net/manual/en/function.fputcsv.php

如文档中所述,您可以...

//put your data in an array like this:
$list = array (
    array('aaa', 'bbb', 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('"aaa"', '"bbb"')
);
//open the output file for writing
$fp = fopen('file.csv', 'w');

//write to the csv file (comma separated, double quote enclosed)
foreach ($list as $fields) {
    fputcsv($fp, $fields,',','"');
}
//close the file
fclose($fp);
于 2013-02-16T20:52:54.040 回答