954,168 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to delete extra commas on csv file with php

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


 $new_line = 'ID, Category, Description'."\n";

 $file = 'dtifeed.csv';

 $old_lines = file($file);
 
  array_unshift($old_lines,$new_line);
  	
$new_content = join('',$old_lines);
 $fp = fopen($file,'r+');
  	
 $write = fwrite($fp, $new_content);
 fclose($fp);





$row = 1;
$handle = fopen("dtifeed.csv", "r+");


while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
   
    $row++;
    for ($c=0; $c < $num; $c++) {
        echo $data[$c] . "\n";

    }
}




fclose($handle);


?>


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

jencinas69
Junior Poster in Training
68 posts since Nov 2007
Reputation Points: 10
Solved Threads: 1
 
$replace = array(',,,"', ',,,x');
$replacewith = ",";
foreach($old_lines as $line)
{
    $newstring = str_replace($replace, $replacewith, $line);
    //process the string
}
R0bb0b
Posting Shark
998 posts since Jun 2008
Reputation Points: 358
Solved Threads: 89
 

Sorry, that last example had a bug.

foreach($old_lines as $line)
{
    $newstring = str_replace(',,,"', ',"', $line);
    $newstring .= str_replace(',,,x', ',x', $newstring);
    //process the string
}
R0bb0b
Posting Shark
998 posts since Jun 2008
Reputation Points: 358
Solved Threads: 89
 

Sorry, that last example had a bug.

foreach($old_lines as $line)
{
    $newstring = str_replace(',,,"', ',"', $line);
    $newstring .= str_replace(',,,x', ',x', $newstring);
    //process the string
}


It did not work here is the complete csv file<>

jencinas69
Junior Poster in Training
68 posts since Nov 2007
Reputation Points: 10
Solved Threads: 1
 

try just doing this then

foreach($old_lines as $line)
{
    $newstring = str_replace(',,,', ',', $line);
    //process the string
}
R0bb0b
Posting Shark
998 posts since Jun 2008
Reputation Points: 358
Solved Threads: 89
 

try just doing this then

foreach($old_lines as $line)
{
    $newstring = str_replace(',,,', ',', $line);
    //process the string
}

Did not work either, here is how I am doing it

<?php


 $new_line = 'ID, Category, Description'."\n";

 $file = 'dtifeed.csv';

 $old_lines = file($file);
 
  array_unshift($old_lines,$new_line);
  	
$new_content = join('',$old_lines);
 $fp = fopen($file,'r+');
  	
 $write = fwrite($fp, $new_content);
 fclose($fp);


foreach($old_lines as $line)
{
    $newstring = str_replace(',,,', ',', $line);
    //process the string
}


$row = 1;
$handle = fopen("dtifeed.csv", "r+");


while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
   
    $row++;
    for ($c=0; $c < $num; $c++) {
        echo $data[$c] . "\n";

    }
}




fclose($handle);
?>
jencinas69
Junior Poster in Training
68 posts since Nov 2007
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You