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] . "<br />\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

Recommended Answers

All 5 Replies

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

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
}

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
<<snip>>

try just doing this then

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

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] . "<br />\n";

    }
}




fclose($handle);
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.