Thanks for all the answers & comments . I did a benchmark of the following functions :
with the following codes ( omitted the while-loop ) :
// fgets ( same code in the question )
$tokens = explode('||', $line);
$a = $tokens[0];
list($b, $c, $d) = explode('|', $tokens[1]);
$result[$a] = array('a' => $a, 'b' => $b, 'c' => $c, 'd' => $d);
// fgetcsv
ini_set('auto_detect_line_endings',TRUE);
list($a, $nouse1, $b, $c, $d, $nouse2, $nouse3) = fgetcsv($fh, 200, '|');
$result[$a] = array('a' => $a, 'b' => $b, 'c' => $c, 'd' => $d);
// stream_get_line
$line = stream_get_line($fh, 200, PHP_EOL);
$tokens = explode('||', $line);
if(count($tokens) != 3) {
continue;
}
$a = $tokens[0];
list($b, $c, $d) = explode('|', $tokens[1]);
$result[$a] = array('a' => $a, 'b' => $b, 'c' => $c, 'd' => $d);
// stream_get_line + str_getcsv
$line = stream_get_line($fh, 200, PHP_EOL);
list($a, $nouse1, $b, $c, $d, $nouse2, $nouse3) = str_getcsv($line, '|');
$result[$a] = array('a' => $a, 'b' => $b, 'c' => $c, 'd' => $d);
// fgets + str_getcsv
$line = fgets($fh);
list($a, $nouse1, $b, $c, $d, $nouse2, $nouse3) = str_getcsv($line, '|');
$result[$a] = array('a' => $a, 'b' => $b, 'c' => $c, 'd' => $d);
They parse the same text file in same path in same testing machine. The line format is :
AAA||BBB|C|DDD||
Here is the result (tested 3 times and take average timings) :
Unexpectedly, fgetcsv() is the slowest. But why ?
sidenote: stream_get_line() only available in PHP 5.
fgets()is the bottleneck.fgetcsv()is, I can't check for errors row by row, and ignore / report the problematic line. Once a malformed line is detected,fgetcsv()will fail.