在新的csv中添加中断

时间:2018-05-06 05:29:27

标签: php csv

我正在尝试在单元格中添加新行,但它在csv中的\ n中显示。我不知道foreach循环有什么问题,但我静静地写在csv中它在csv中生成得很好ex: fputcsv($ fh,数组('D',“E \ nF \ nG”,'H'),“\ t”);

$classreport = array(
    array(
        'name' => 'Vick',
        'std' => 'A',
        'marks' => array(10, 20, 30, 40)
    ),
    array(
        'name' => 'Josh',
        'std' => 'B',
        'marks' => array(20, 40)
    ),
);


$fh = fopen('test3.csv', 'w+');
fwrite($fh, "sep=\t" . "\r\n");
$colums=array('name','std','mark');
fputcsv($fh, $colums, "\t");
foreach($classreport as $student) {

   $tt=implode(',',$student['marks']);
  fputcsv($fh, array($student['name'], $student['std'],trim(str_replace(',','\n',$tt)) ), "\t");

} 
fclose($fh); 

enter image description here

1 个答案:

答案 0 :(得分:1)

因为single quote并不代替" \ n"到换行符。你需要像这样双引号:

foreach($classreport as $student) {
  $tt=implode(',',$student['marks']);
  fputcsv($fh, array($student['name'],$student['std'],trim(str_replace(',',"\n",$tt)) ), "\t");
}