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