Re: Parse Question Using list()
| From: | Ben Dunlap | Date: | Fri, 02 Oct 2009 00:02:25 +0000 |
| Subject: | Re: Parse Question Using list() | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-298454@lists.php.net to get a copy of this message | ||
> $line = fgets($handle);
>
> list($col1, $col2, $col3) = $line;
[8<]
> echo "c1 is $col1 and c2 is $col2 and c3 is $col3".'<br>'; // 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