I need to replace the values only from the third column in my csv. And no where else. Column 3 of my csv contains only a 'J' or a'N' and i need to change them to '5' or '0'. But right now i it changes always all found N's and J#s in the whole file. But i need it only updated in the third column.
I use the following code to open and rewrite my csv. But it writes always all found Letters to 5 or 0.
$csvfile = 'csvfile.csv';
if (file_exists($csvfile)) {
unlink($csvfile);
echo "Alte $csvfile entfernt!";
} else {
echo "Keine alte $csvfile vorhanden";
}
$row = 0;
$fp = fopen('csvfile.csv', 'w');
if (($handle = fopen("oldCSV.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 200000, ",")) !== FALSE) {
$num = count($data);
$dataold = ['J', 'N'];
$datanew = ['5', '0'];
echo "<p> $num Felder in Zeile $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
fputs($fp, str_replace($dataold, $datanew, $data[$c] . "\n"));
}
}
}
fclose($fp);