note 40388 deleted from function.fgetcsv by irchtml
| From: | irchtml@php.net | Date: | Mon, 08 Mar 2004 10:52:13 +0000 |
| Subject: | note 40388 deleted from function.fgetcsv by irchtml | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-66428@lists.php.net to get a copy of this message | ||
Note Submitter: bu@orbitel.bg
----
Considering my latest experience with the fgetcsv() function, I advice you to write a replacement of
it since it's extremely slow.
I used it to parse a 15,000 line csv but it took 2 and a half minutes to parse it with fgetcsv(). So
I used Perl regular expressions to parse it and the result was amazing - 7 seconds execution time.
Just as a note: Same functionallity written in Perl gave me 7 seconds too - so I'm almost sure
I got the best possible result with my replacement.
Here's the sample code that I used - feel free to send me comments on it.
<?php
function fgetcsvfromline($line, $columnCount) {
$matches = array();
$regExp = '/^';
for ($i = 0; $i < $columnCount; $i++) {
$regExp .= "(\"?)(.*?)\\".(2*$i + 1).",";
}
$regExp = substr($regExp, 0, (strlen($regExp) - 1))."/";
if (preg_match($regExp, $line, $matches)) {
return $matches;
}
return 0;
}
if ($fileLines = @file($filePath, "r")) {
foreach ($fileLines as $line) {
if ($csvData = fgetcsvfromline($line, 3)) { // 3 represents the number of columns I want to parse
// here you can process the extracted data
}
}
}
?>