I want to parse, process and then overwrite only 1 specific column in a csv file, I’m fine with the first two steps but am having issues with the third. Here’s what I have so far:
<?php
$fh = fopen("data.csv", "r");
$row = 1;
while (($data = fgetcsv($fh, 1024, ",")) !== FALSE) {
$get = $data[2];
$uc = ucwords(strtoupper($get));
echo $uc;
}
?>
That displays fine, I just need the processed content to be written back into it’s original column. I know this is done with fputcsv, but I can’t seem to get it to work:
$fp = fopen('data.csv', 'w');
fputcsv($fp, $uc, ",");
fclose($fp);
That just wipes the entire file. Same thing when using fwrite. Any ideas?
Thanks.
Please note that both functions work for
lines, not for the whole file.So you will have to rebuild it when you write out, something like:
Untested