Write CSV To File Without Enclosures In PHP

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

提交回复
热议问题