View Single Post
Join Date: Nov 2007
Posts: 68
Reputation: jencinas69 is an unknown quantity at this point 
Solved Threads: 1
jencinas69's Avatar
jencinas69 jencinas69 is offline Offline
Junior Poster in Training

Re: how to delete extra commas on csv file with php

 
0
  #6
Jun 12th, 2008
Originally Posted by R0bb0b View Post
try just doing this then
  1. foreach($old_lines as $line)
  2. {
  3. $newstring = str_replace(',,,', ',', $line);
  4. //process the string
  5. }
Did not work either, here is how I am doing it

  1. <?php
  2.  
  3.  
  4. $new_line = 'ID, Category, Description'."\n";
  5.  
  6. $file = 'dtifeed.csv';
  7.  
  8. $old_lines = file($file);
  9.  
  10. array_unshift($old_lines,$new_line);
  11.  
  12. $new_content = join('',$old_lines);
  13. $fp = fopen($file,'r+');
  14.  
  15. $write = fwrite($fp, $new_content);
  16. fclose($fp);
  17.  
  18.  
  19. foreach($old_lines as $line)
  20. {
  21. $newstring = str_replace(',,,', ',', $line);
  22. //process the string
  23. }
  24.  
  25.  
  26. $row = 1;
  27. $handle = fopen("dtifeed.csv", "r+");
  28.  
  29.  
  30. while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  31. $num = count($data);
  32.  
  33. $row++;
  34. for ($c=0; $c < $num; $c++) {
  35. echo $data[$c] . "<br />\n";
  36.  
  37. }
  38. }
  39.  
  40.  
  41.  
  42.  
  43. fclose($handle);
  44. ?>
Reply With Quote