DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   PHP (http://www.daniweb.com/forums/forumdisplay.php?f=17)
-   -   how to delete extra commas on csv file with php (http://www.daniweb.com/forums/showthread.php?t=128859)

jencinas69 Jun 11th, 2008 12:21 pm
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] . "<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

R0bb0b Jun 11th, 2008 7:33 pm
Re: how to delete extra commas on csv file with php
 
$replace = array(',,,"', ',,,x');
$replacewith = ",";
foreach($old_lines as $line)
{
    $newstring = str_replace($replace, $replacewith, $line);
    //process the string
}

R0bb0b Jun 12th, 2008 12:26 am
Re: how to delete extra commas on csv file with php
 
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
}

jencinas69 Jun 12th, 2008 11:52 am
Re: how to delete extra commas on csv file with php
 
Quote:

Originally Posted by R0bb0b (Post 625726)
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>>

R0bb0b Jun 12th, 2008 11:56 am
Re: how to delete extra commas on csv file with php
 
try just doing this then
foreach($old_lines as $line)
{
    $newstring = str_replace(',,,', ',', $line);
    //process the string
}

jencinas69 Jun 12th, 2008 12:02 pm
Re: how to delete extra commas on csv file with php
 
Quote:

Originally Posted by R0bb0b (Post 626048)
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);
?>


All times are GMT -4. The time now is 6:50 am.