Write CSV To File Without Enclosures In PHP

前端 未结 14 791
谎友^
谎友^ 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:40

    I use tricky way to remove double quote, but only in Linux

    ....
    fputcsv($fp, $product_data,"\t");
    ....
    shell_exec('sed -i \'s/"//g\' /path/to/your-file.txt ');
    
    0 讨论(0)
  • 2020-12-01 07:41

    This is what I use to put standard CSV into an array...

    function csv_explode($delim=',', $str, $enclose='"', $preserve=false){
            $resArr = array();
            $n = 0;
            $expEncArr = explode($enclose, $str);
            foreach($expEncArr as $EncItem){
                    if($n++%2){
                            array_push($resArr, array_pop($resArr) . ($preserve?$enclose:'') . $EncItem.($preserve?$enclose:''));
                    }else{
                            $expDelArr = explode($delim, $EncItem);
                            array_push($resArr, array_pop($resArr) . array_shift($expDelArr));
                            $resArr = array_merge($resArr, $expDelArr);
                    }
            }
            return $resArr;
    } 
    

    You can then output whatever you want in a foreach loop.

    0 讨论(0)
  • 2020-12-01 07:41

    Chose the solution depends on your application. For some case own code for csv is needed. In case 1 (See result chr0), case 2 (chr127) and case 3 (chr127) the data will be modified(bad thing). Instead use something like this, thanks @oops:

    <?php
    $fields = array(
        "field 1","field 2","field3hasNoSpaces"
    );
    fputs(STDOUT, implode(',', $fields)."\n");
    
    
    case 1. `fputcsv($f, $array, $delimiter, car(0))`
    See result [chr0][1]
    case 2. `fputcsv($f, $array, $delimiter, car(127))`
    [chr127][2]
    case 3. `fputcsv($f, $array, $delimiter, ' ')`
    [onespace][3]
    case 4. Own code:
    [oops](https://stackoverflow.com/a/1904945)
     or [Ansyori](https://stackoverflow.com/a/51981613)
     or [zeros-and-ones](https://stackoverflow.com/a/38778459)
    produces better results.
    
    0 讨论(0)
  • 2020-12-01 07:46

    Well car(0) didn't work out as the NULL value will most likely choke most csv parsers.

    I ended using fputcsv() to build the initial file, then went through and removed all quotes. Elegant? Maybe not, but it got the job done :).

    0 讨论(0)
  • 2020-12-01 07:46

    Using "chr(0)" adds a character to the file. After doing some investigation and checking the ASCII Table, I've ended up using the following

    fputcsv($fp, $aLine, $sDelimiter, chr(127));
    

    No issues with extra characters.

    0 讨论(0)
  • 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)

    0 讨论(0)
提交回复
热议问题