f.almeida
f.almeida

Reputation: 3

enclosing csv fields with quotes

i need to enclose my data in " " already tried several things but i only mess up my data

i need the output csv to be formated:
"data","data","data"
the code im using and displayed below was copied from another question here.

if (($handle = fopen('DIRTY_'.$val['spider'].'.csv', 'r')) !== false) {
    // read each line into an array
    while (($data = fgetcsv($handle, 8192, ",")) !== false) {
        // build a "line" from the parsed data
        $line = join("," , $data );

        // if the line has been seen, skip it
        if (isset($lines[$line])) continue;

        // save the line
        $lines[$line] = true;
    }
    fclose($handle);
}


$contents = '';
foreach ($lines as $line => $bool) $contents .= $line . "\r\n";


file_put_contents($val['spider'].".csv", $contents);

}

Upvotes: 0

Views: 226

Answers (1)

Halcyon
Halcyon

Reputation: 57709

Use fgetcsv and fputcsv. It will do escaping for you.

It's also worth mentioning that CSV is not a well defined standard. Sometimes it's comma separated, sometimes it's semicolons and sometimes it's tabs. Sometimes " are always present, sometimes only when you have newlines.

Upvotes: 1

Related Questions