search CVS file (perharps a bug)
| From: | Laurent Laville | Date: | Sat, 30 Apr 2005 10:02:50 +0000 |
| Subject: | search CVS file (perharps a bug) | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-37458@lists.php.net to get a copy of this message | ||
Hi,
As i'm not sure if it's a bug a not (perharps i don't use the PEAR::File package well) tell me if i'm wrong or if i should open a bug entry ?
Well, recently i've downloaded and used the File-1.2.0 package version to read and search data inside a cvs file.
my cvs file is a 3 fields separated by point-comma, such as :
54;Poitou-Charentes;0
82;Rhône-Alpes;1
01;Guadeloupe;0
02;Martinique;0
03;Guyane;0
04;La Réunion;0
11;Ile-de-France;1
21;Champagne-Ardenne;0
... and many more
i've written a function to open a cvs file and search an entry:
this function is:
<?php
require_once 'File/CSV.php';
$filename = 'LISTE_REG.txt';
$conf = array('fields' => 3, 'sep' => ';', 'crlf' => '\r\n');
function searchReg($regKey)
{
global $filename, $conf;
$data = array();
while ($r = File_CSV::read($filename, $conf)) {
if (trim($r[0]) != $regKey) {
continue;
}
$data[] = array('COD_REG' => trim($r[0]),
'LIB_REG' => trim($r[1]));
break;
}
File::close($filename, $conf);
return $data;
}
$data = searchReg(11);
print_r($data); // [1]echo '<hr /><br />'; $data = searchReg(54);
print_r($data); // [2]?> First attempt [1] gave the good result, but second attempt gave an empty array. The File::close() method seems not to close the file. File pointer keep the last position and a new search try give a wrong result. Hope i'm clear as enough regards Laurent Laville