Re: fgetcsv
| From: | Daniel Brown | Date: | Wed, 09 Jan 2008 23:02:09 +0000 |
| Subject: | Re: fgetcsv | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-266981@lists.php.net to get a copy of this message | ||
On Jan 9, 2008 5:35 PM, Danny Brow <maillist@fullmotions.com> wrote:
> Hi Everyone,
>
> I'm trying to compare a value to the first field in a csv fILE (example
> of the data below). Using while takes too long and I can't figure out
> how to compare just one row at a time. I've tried some variations of the
> following.
>
>
> //Common for all trials
> $demoID = fopen("newDemoID.csv", "r");;
> $ID = "43";
>
> $data = fgetcsv($demoID);
>
>
>
> First try with while:
>
> /*
> while ($data) {
> if ($data[0] == $wolfID) {
> print $data[1] . "," . $data[2] . "," . $data[3] .
> ".\n";
> }
> }
> */
As an alternative, you could try this:
<?
$wolfID = "43";
$data = file_get_contents($filename);
$line = explode("\n",$data);
for($i=0;$i<count($line);$i++) {
$field = explode(",",$line[$i]); // Replace the comma with your separator.
$match =
str_replace("'",'',str_replace('"','',$field[0]));
//
Strip quotes, if they exist.
if($match == $wolfID) {
echo $line[$i]."\n";
}
}
?>
--
</Dan>
Daniel P. Brown
Senior Unix Geek and #1 Rated "Year's Coolest Guy" By Self Since 1979.