timr Posted March 21, 2011 Share Posted March 21, 2011 Hello All, I am trying to modify a value found in a csv file and I can't seem to figure this out. I already looked into fputcsv and cannot seem to make it work to just modify a value. I have a script now that outputs the csv into a table and displays a count for how many rows there are. I am parsing through a very large file and (for example) I want to replace all instances of the word "Norway" with "it works". Here's what I have so far: <?php $filename='Test.txt'; $count=0; $c=0; echo '<table border=1>'; $file_handle = fopen($filename, "r+"); while (!feof($file_handle) ) { $parts = fgetcsv($file_handle); echo '<tr><td>'.$count.'</td>'; while($c<=3) { if($parts[$c]=='Norway') { $parts[$c]='It works'; //code to modify the csv value in the file } echo '<td>'. $parts[$c] .'</td>'; $c++; } $c=0; "</tr>"; $count++; } fclose($file_handle); echo '</table>'; ?> I have looked all over and can't find any examples of how to do this. Any help would be greatly apprecieated!!! Quote Link to comment https://forums.phpfreaks.com/topic/231226-modify-a-value-in-csv-file/ Share on other sites More sharing options...
sunfighter Posted March 21, 2011 Share Posted March 21, 2011 Your code: if($parts[$c]=='Norway') { $parts[$c]='It works'; //code to modify the csv value in the file } works fine for the substitution your looking for. Your code to display info in a table is a little messed. Does your cvs file contain line feeds and where are they placed? Quote Link to comment https://forums.phpfreaks.com/topic/231226-modify-a-value-in-csv-file/#findComment-1190131 Share on other sites More sharing options...
timr Posted March 21, 2011 Author Share Posted March 21, 2011 Hello and thanks for the reply. My code: if($parts[$c]=='Norway') { $parts[$c]='It works'; //code to modify the csv value in the file } Only modifies the variable. I am actually looking to modify the csv file itself. My real csv file is extremely large so here is an example of how it is setup: "Bruno","Gammit","Wind" "John","Stewy","Norway" Any idea on how I could modify the csv file itself to replace the searched value (in my example "Norway") with the new value (in my example "it works")? All help is greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/231226-modify-a-value-in-csv-file/#findComment-1190139 Share on other sites More sharing options...
sunfighter Posted March 22, 2011 Share Posted March 22, 2011 Oh. To modify a file 'we don't need no stinking cvs'. You need to open your input file with a read attribute and place the ENTIRE file into a string. Then make your changes to the string. Re-open the file for write, and write to it. This is php code. $fname = "Test.txt"; $fhandle = fopen($fname,"r"); $content = fread($fhandle,filesize($fname)); $content = str_replace("oldword", "newword", $content); $fhandle = fopen($fname,"w"); fwrite($fhandle,$content); fclose($fhandle); Quote Link to comment https://forums.phpfreaks.com/topic/231226-modify-a-value-in-csv-file/#findComment-1190667 Share on other sites More sharing options...
timr Posted March 26, 2011 Author Share Posted March 26, 2011 Thank you very much for your reply! I was in the mindset that there must be a specific function just for csv. However, your way makes WAY more sense and works perfectly for what I am trying to achieve! SOLVED! Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/231226-modify-a-value-in-csv-file/#findComment-1192381 Share on other sites More sharing options...
neeshedh Posted May 7, 2013 Share Posted May 7, 2013 i have same problem i want to select only first and second row. top to bottom complete. and want to change current to 1 and discontinued to 2. but above code change complete csv values. ( Script only change first and second row current and discontinued values not other rows ) Status | Other Rows ---------- | ok Discuntinued | ok Current | ok Discuninued | no This forum not allow me to attach image that's y above csv sample. Thank You. Quote Link to comment https://forums.phpfreaks.com/topic/231226-modify-a-value-in-csv-file/#findComment-1429005 Share on other sites More sharing options...
Barand Posted May 8, 2013 Share Posted May 8, 2013 SOLVED! There is a "Mark Solved" button Quote Link to comment https://forums.phpfreaks.com/topic/231226-modify-a-value-in-csv-file/#findComment-1429006 Share on other sites More sharing options...
neeshedh Posted May 8, 2013 Share Posted May 8, 2013 i have same problem i want to select only first and second row. top to bottom complete. and want to change current to 1 and discontinued to 2. but above code change complete csv values. ( Script only change first and second row current and discontinued values not other rows ) Status | Other Rows ---------- | ok Discuntinued | ok Current | ok Discuninued | no This forum not allow me to attach image that's y above csv sample. Thank You. Quote Link to comment https://forums.phpfreaks.com/topic/231226-modify-a-value-in-csv-file/#findComment-1429034 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.