Parse Question Using list()
Parse Question Using list()
am 02.10.2009 01:32:46 von cool
newbie import csv question
file is like:
stuff1,stuff2,stuff3
stuff1,stuff2,stuff3
stuff1,stuff2,stuff3
stuff1,stuff2,stuff3
etc.
Problem: when I try to parse out the 3 fields and display them using
list() it just gets just 1st char of each field ...
Q: How do I get it to set $col1 - 2 & $col3 to the full contents of
each for each line (not just 1st char)?
// on a mac but with windows linefeed returns in file
$handle = fopen("file:///path/_impfool/test.csv", "r");
while (!feof ($handle))
{
$line = fgets($handle);
list($col1, $col2, $col3) = $line;
echo $line.''.' line
'; // this shows the whole line ok
echo "c1 is $col1 and c2 is $col2 and c3 is $col3".'
'; // this
shows just 1st char of each field
}
?>
Thanks,
cool@hosting4days.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Parse Question Using list()
am 02.10.2009 02:02:25 von Ben Dunlap
> $line = fgets($handle);
>
> list($col1, $col2, $col3) = $line;
[8<]
> echo "c1 is $col1 and c2 is $col2 and c3 is $col3".'
'; // this shows
> just 1st char of each field
That's odd, I would have expected $col1, $col2, and $col3 to be NULL.
That's what I get when I try to assign a string to list(). It expects
a PHP array.
You could tackle this in a couple of different ways. Either split your
string into an array first:
$line = fgets($handle);
$columns = explode(",", trim($line));
list($col1,$col2,$col3) = $columns;
Or look at using fgetcsv(), which will save you a step or two:
http://php.net/manual/en/function.fgetcsv.php
Ben
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Parse Question Using list()
am 02.10.2009 03:58:55 von cool
On Oct 1, 2009, at 5:02 PM, Ben Dunlap wrote:
> You could tackle this in a couple of different ways. Either split your
> string into an array first:
>
> $line = fgets($handle);
> $columns = explode(",", trim($line));
Thanks Ben - the explode() command worked great!
---------
Now a bit of another problem
I'm exporting from another database (mac) to a csv file then a quick
import to excel 2004 (mac) for some cleaning...
before the excel import, some date fields look like "2009-9-29
11:21:37" = good for sql import
but excel does an auto reformat to
9/29/2009 11:21:37 AM = not good for sql import
Q: any way to turn this auto reformat off in excel and keep it the
way I had it? (I saw nothing in pref's)
Thanks,
cool@hosting4days.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Parse Question Using list()
am 02.10.2009 06:50:00 von List Manager
cool@hosting4days.com wrote:
>
> On Oct 1, 2009, at 5:02 PM, Ben Dunlap wrote:
>
>> You could tackle this in a couple of different ways. Either split your
>> string into an array first:
>>
>> $line = fgets($handle);
>> $columns = explode(",", trim($line));
>
> Thanks Ben - the explode() command worked great!
>
Use the tool that PHP provides for such problems.
http://php.net/fgetcsv
> ---------
>
> Now a bit of another problem
>
> I'm exporting from another database (mac) to a csv file then a quick
> import to excel 2004 (mac) for some cleaning...
>
> before the excel import, some date fields look like "2009-9-29
> 11:21:37" = good for sql import
>
> but excel does an auto reformat to
>
> 9/29/2009 11:21:37 AM = not good for sql import
>
>
> Q: any way to turn this auto reformat off in excel and keep it the way I
> had it? (I saw nothing in pref's)
>
try using strtotime() on that field and see if it works.
http://php.net/strtotime
I'm pretty sure that function will handle it.
>
> Thanks,
> cool@hosting4days.com
>
>
>
>
>
>
--
Jim Lucas
"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."
Twelfth Night, Act II, Scene V
by William Shakespeare
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Parse Question Using list()
am 02.10.2009 15:56:20 von Gerardo Benitez
--00504502ec63f740dc0474f420e2
Content-Type: text/plain; charset=ISO-8859-1
>
> Use the tool that PHP provides for such problems.
>
> http://php.net/fgetcsv
fgetcsv is very useful, here a example:
$row = 1;
/* load file*/
$handle = fopen("log.csv", "r");
/* read line by line */
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "
$num fields in line $row:
\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "
\n";
}
}
fclose($handle);
?>
--
Gerardo Benitez
--00504502ec63f740dc0474f420e2--