Jump to content

editing CSV


Clarissa

Recommended Posts

I'm trying to find a suitable alternative to file_put_contents for the purpose of overwriting a column of entries in a csv file.

 

I'm using preg_match_all to extract certain words out of one column and want them to replace their original strings without affecting the rest of the file. The following code gets the job done but at the expense of the other columns i.e. they get wiped:

 

<?php
$fh = fopen('data.csv', 'r');
$str = fread ($fh, filesize('data.csv'));
preg_match_all('/%REGEX%/', $str, $matches);
file_put_contents('data.csv', implode("\r\n", $matches[1])); 
fclose($fh);
?>

 

I've been toying around with it and this is the closest that comes to what I'm after:

 

<?php
$fh = fopen('data.csv', 'r');
$str = fread ($fh, filesize('data.csv'));
preg_match_all('/%REGEX%/', $str, $matches);
foreach($matches[1] as $value)
$output = preg_replace('/%REGEX%/', $value, $str);
$filename = "data.csv";
$write = $output;
$filehandle = fopen($filename, 'w');
fwrite($filehandle, $write);
fclose($filehandle);
?>

 

That preserves the other columns and data but only the last extracted word gets repeatedly written, throughout the entire column, on every single row. Arrays aren't yet my forte so I'm guessing the solution is somewhere in that dept.

 

Is there maybe a simpler way to do this, perhaps with preg_replace? Also, the csv file's integrity gets messed up if a non-match results in a deleted cell, so a substitute entry is required if no match is found.

 

Any ideas? Thanks.

Link to comment
https://forums.phpfreaks.com/topic/193213-editing-csv/
Share on other sites

I would suggest preg_replace_callback() is what might help you, where you can define a callback function to handle the replacement of the matches.

 

http://de3.php.net/manual/en/function.preg-replace-callback.php

Oh wow, that was exactly what I needed, it does everything I require without compromising the other data, thanks so much!!! :thumb-up:

 

why not:

file_get_contents

preg_replace

file_put_contents

 

 

HTH

Teamatomic

Thanks, but preg_replace_callback() did the trick! However, you can help this silly gal out with another, obviously simple problem:

 

I want to parse, process and then overwrite only 1 specific column in the file, I'm fine with the first two steps but am having issues with the third. Here's what I have so far:

 

<?php
$fh = fopen("data.csv", "r");
$row = 1;
while (($data = fgetcsv($fh, 1024, ",")) !== FALSE) {
$get = $data[2];
$uc = ucwords(strtoupper($get));
echo $uc;
echo "</br>";
}
?>

 

That displays fine, I just need it to be written back into it's original column. I know this is done with fputcsv, but I can't seem to get it to work:

 

$fp = fopen('data.csv', 'w');
fputcsv($fp, $uc, ",");
fclose($fp);

 

That wipes the file. :shrug:

Link to comment
https://forums.phpfreaks.com/topic/193213-editing-csv/#findComment-1017922
Share on other sites

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.