Write CSV To File Without Enclosures In PHP

前端 未结 14 806
谎友^
谎友^ 2020-12-01 07:11

Is there a native function or solid class/library for writing an array as a line in a CSV file without enclosures? fputcsv will default to \" if no

14条回答
  •  不思量自难忘°
    2020-12-01 07:47

    Whats wrong with good old fwrite()?

    function encloseString($field){
        return ($field) ? '"' . $field . '"' : null;
    }
    
    $delimiter = ';';
    $data = ["data_field_1", "data_field_2", "data_field_3"];
    
    $fp = fopen("some-file.csv", 'w');
    
    for($i = 0;$i<100000;$i++) {
        fwrite($fp, implode($delimiter, array_map('encloseString', $data) . "\n");
    }
    
    fclose($fp);
    

    (Obviously you need to make sure the data in $data is escaped first)

提交回复
热议问题