Jump to content

Modify a value in csv file


timr

Recommended Posts

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:

 

<?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!!!

Link to comment
https://forums.phpfreaks.com/topic/231226-modify-a-value-in-csv-file/
Share on other sites

Your code:

  if($parts[$c]=='Norway') {

  $parts[$c]='It works';

  //code to modify the csv value in the file

  }

works fine for the substitution your looking for. Your code to display info in a table is a little messed. Does your cvs file contain line feeds and where are they placed?

Hello and thanks for the reply.

 

My code:

 

  if($parts[$c]=='Norway') {

  $parts[$c]='It works';

  //code to modify the csv value in the file

  }

 

Only modifies the variable. I am actually looking to modify the csv file itself.

 

My real csv file is extremely large so here is an example of how it is setup:

 

"Bruno","Gammit","Wind"

"John","Stewy","Norway"

 

Any idea on how I could modify the csv file itself to replace the searched value (in my example "Norway") with the new value (in my example "it works")?

 

All help is greatly appreciated!

Oh.  :o

 

To modify a file 'we don't need no stinking cvs'. You need to open your input file with a read attribute and place the ENTIRE file into a string. Then make your changes to the string. Re-open the file for write, and write to it.

 

This is php code.

$fname = "Test.txt";
$fhandle = fopen($fname,"r");
$content = fread($fhandle,filesize($fname));

$content = str_replace("oldword", "newword", $content);

$fhandle = fopen($fname,"w");
fwrite($fhandle,$content);
fclose($fhandle);

 

  • 2 years later...

i have same problem i want to select only first and second row. top to bottom complete. and want to change current to 1 and discontinued to 2. but above code change complete csv values. ( Script only change first and second row current and discontinued values not other rows )

 

 

Status             |        Other Rows

----------           |          ok

Discuntinued   |          ok

Current           |          ok

Discuninued    |         no

 

This forum not allow me to attach image that's y above csv sample.

 

Thank You.

i have same problem i want to select only first and second row. top to bottom complete. and want to change current to 1 and discontinued to 2. but above code change complete csv values. ( Script only change first and second row current and discontinued values not other rows )

 

 

Status             |        Other Rows

----------           |          ok

Discuntinued   |          ok

Current           |          ok

Discuninued    |         no

 

This forum not allow me to attach image that's y above csv sample.

 

Thank You.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.