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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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);

 

Link to comment
Share on other sites

Thank you very much for your reply!

 

I was in the mindset that there must be a specific function just for csv. However, your way makes WAY more sense and works perfectly for what I am trying to achieve!

 

SOLVED!

 

Thanks again.

Link to comment
Share on other sites

  • 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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.