Clarissa Posted February 24, 2010 Share Posted February 24, 2010 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. Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 24, 2010 Share Posted February 24, 2010 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 Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 24, 2010 Share Posted February 24, 2010 why not: file_get_contents preg_replace file_put_contents HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
Clarissa Posted February 25, 2010 Author Share Posted February 25, 2010 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!!! 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.