note 40388 added to function.fgetcsv

From: Date: Tue, 02 Mar 2004 12:25:44 +0000
Subject: note 40388 added to function.fgetcsv
Groups: php.notes 
Request: Send a blank email to php-notes+get-66096@lists.php.net to get a copy of this message
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 } } } ?> ---- Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+40388 Delete -- http://master.php.net/manage/user-notes.php?action=delete+40388&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+40388&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#66096) next »