Modify a value in csv file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
timr
Forum Newbie
Posts: 1
Joined: Sat Mar 19, 2011 4:18 pm

Modify a value in csv file

Post by timr »

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:

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>';
?>
I have looked all over and can't find any examples of how to do this. Any help would be greatly apprecieated!!!
Post Reply