Modify a value in csv file
Posted: Sat Mar 19, 2011 4:39 pm
Hello All,
I am trying to modify a value found in a csv file and I can't seem to figure this out. I already looked into fputcsv and cannot seem to make it work to just modify a value. I have a script now that outputs the csv into a table and displays a count for how many rows there are. I am parsing through a very large file and (for example) I want to replace all instances of the word "Norway" with "it works". Here's what I have so far:
I have looked all over and can't find any examples of how to do this. Any help would be greatly apprecieated!!!
I am trying to modify a value found in a csv file and I can't seem to figure this out. I already looked into fputcsv and cannot seem to make it work to just modify a value. I have a script now that outputs the csv into a table and displays a count for how many rows there are. I am parsing through a very large file and (for example) I want to replace all instances of the word "Norway" with "it works". Here's what I have so far:
Code: Select all
<?php
$filename='Test.txt';
$count=0;
$c=0;
echo '<table border=1>';
$file_handle = fopen($filename, "r+");
while (!feof($file_handle) ) {
$parts = fgetcsv($file_handle);
echo '<tr><td>'.$count.'</td>';
while($c<=3)
{
if($parts[$c]=='Norway') {
$parts[$c]='It works';
//code to modify the csv value in the file
}
echo '<td>'. $parts[$c] .'</td>';
$c++;
}
$c=0;
"</tr>";
$count++;
}
fclose($file_handle);
echo '</table>';
?>