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

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

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.