Jump to content

fgetcsv / fputcsv help ... can't figure out how to do both


johnwayne77

Recommended Posts

ok, i have :

$fname = "data.csv";
$fhandle = fopen($fname,"r");


while (($data = fgetcsv($fhandle, 1000, ",")) !== FALSE)
{
    
$oldcat = array("\"40\"", "\"52\"", "\"54\"");
$newcat  = array("100x", "1000x", "10000x");

$data[2] = str_replace($oldcat, $newcat, $data[2]);

}

fclose($fhandle);

 

so, i found column 3, searched and replaced the values i want but now... how do i apply the changes to the same csv file ? any ideas? would really be appreciated as i'm running my brains out

 

thanks

Link to comment
Share on other sites

<?php
$handle = fopen('data.csv');
$lines = array();
while ($data = fgetcsv($handle)) {
   $oldcat = array("\"40\"", "\"52\"", "\"54\"");
   $newcat  = array("100x", "1000x", "10000x");
   $data[2] = str_replace($oldcat, $newcat, $data[2]);
   $lines[] = $data;
}
while ($line = each($lines)) {
   fputcsv($handle, $line);
}
fclose($handle);
?>

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.