943,685 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 28813
  • PHP RSS
Jun 11th, 2008
0

how to delete extra commas on csv file with php

Expand Post »
Hello I have this csv file


414060,440A,,,"QUECHAN INDIAN TRIBE SECRETARY- HIGHER EDUCATION DEPARTMENT Salary $8.00 per hour Position Closes May 9th, 2008 Position is responsible for providing administrative support to the Higher Education Department. Preparing office reports, maintaining the fi"
414297,445B,,," Come work in a great team environment. NOW HIRING FT Activities Assistant Ask for Elga Martinez FT HOUSEKEEPER Benefits available. Inquire within. 2222 S. Ave A., Yuma Ask for Theresa Cardenas"
411963,470A,,,x x x x x x x x
413675,470A,,,"NEWSPAPER DELIVERY Arizona Republic has opening for newspaper delivery to homes and store accounts. Must have reliable auto, early morning hours, approximately 3 1/2 hours, 7 days per week. Earn $800 - $1,000 monthly. For more information call 928-344-60"
414041,470A,,,"COUPLE WANTED to manage & maintain well kept, 146 space, Yuma RV Resort. F/T, Residence & Salary 928-782-2222"

With this php file I am writting the headers to this csv file so it can be parse into xml

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

My problem is the extra commas in the csv before the descrption, How could I delimit just to 1 comma before the description on each ad with php?

Thank you
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
jencinas69 is offline Offline
68 posts
since Nov 2007
Jun 11th, 2008
0

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

PHP Syntax (Toggle Plain Text)
  1. $replace = array(',,,"', ',,,x');
  2. $replacewith = ",";
  3. foreach($old_lines as $line)
  4. {
  5. $newstring = str_replace($replace, $replacewith, $line);
  6. //process the string
  7. }
Last edited by R0bb0b; Jun 11th, 2008 at 7:36 pm.
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Jun 12th, 2008
0

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

Sorry, that last example had a bug.
PHP Syntax (Toggle Plain Text)
  1. foreach($old_lines as $line)
  2. {
  3. $newstring = str_replace(',,,"', ',"', $line);
  4. $newstring .= str_replace(',,,x', ',x', $newstring);
  5. //process the string
  6. }
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Jun 12th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by R0bb0b ...
Sorry, that last example had a bug.
PHP Syntax (Toggle Plain Text)
  1. foreach($old_lines as $line)
  2. {
  3. $newstring = str_replace(',,,"', ',"', $line);
  4. $newstring .= str_replace(',,,x', ',x', $newstring);
  5. //process the string
  6. }

It did not work here is the complete csv file
<<snip>>
Last edited by Nick Evan; Nov 14th, 2011 at 7:03 am.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
jencinas69 is offline Offline
68 posts
since Nov 2007
Jun 12th, 2008
0

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

try just doing this then
PHP Syntax (Toggle Plain Text)
  1. foreach($old_lines as $line)
  2. {
  3. $newstring = str_replace(',,,', ',', $line);
  4. //process the string
  5. }
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Jun 12th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by R0bb0b ...
try just doing this then
PHP Syntax (Toggle Plain Text)
  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

PHP Syntax (Toggle Plain Text)
  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. ?>
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
jencinas69 is offline Offline
68 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: parse error
Next Thread in PHP Forum Timeline: Using preg_match to block spam URLs in PHP forms





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC