Page 1 of 1

data seperated by commas

Posted: Mon Mar 24, 2003 1:01 pm
by penguinboy
I have data from database seperated by commas

the format:
(lastname),,(firstname),(middlename),(lastname),,(firstname),(middlename),(lastname),,(firstname),(middlename)

I want to be able to pull out
(lastname),,(firstname),(middlename)

or possibly put a /n in
(lastname),,(firstname),(middlename)/n
(lastname),,(firstname),(middlename)/n

example:
SMITH,,JOHN,DAYTON,CUMMINGS,,MIKE,ALLEN,CLANTON,,HOMER,WILL,WILSON,,MORGAN,JOE

I just don't know how to go about doing this.

Thanks for any help
--pb

Posted: Mon Mar 24, 2003 1:21 pm
by mchaggis
RFTM ;)
http://www.php.net/manual/en/function.fgetcsv.php

That works best with 1 record per line:
lastname),,(firstname),(middlename)/n
(lastname),,(firstname),(middlename)/n


alternatively:

Code: Select all

$csv = file('db.csv');
$records = 0;

for($x = 0; $x < count($csv); $x++) &#123;
   $currentline = ereg_replace('&#1111;\n\r]+','',$csv&#1111;$x]);

   $data = split(',' $currentline);
   while( sizeof($data) > 0 ) &#123;
        $record&#1111;$records]&#1111;lastname] = array_shift($data);
        $record&#1111;$records]&#1111;blank] = array_shift($data);
        $record&#1111;$records]&#1111;firstname] = array_shift($data);
        $record&#1111;$records]&#1111;middlename] = array_shift($data);
        $records++;
   &#125;

&#125;