我最终为自己创建了一个解决方案,但我很好奇是否有其他人可以针对更大的数据集进行测试?
使用str_getcsv 解析行的平均时间为 0.0014 秒。
使用此代码解析平均需要 0.0002 秒。
它肯定可以使用额外的工作来提供更大的灵活性,但是对于带有引用值的简单 CSV,这对我的目的来说很好。
function _csv2array($line) {
$ret = [0 => '']; //Start with an empty array
$idx = 0; //First index
$lastpos = 0; //No commas found yet
while (($pos = strpos($line, ',', $lastpos)) !== FALSE) { //While we find another comma
$ret[$idx].= substr($line, $lastpos, $pos-$lastpos); //Add it to our current index
if (substr($ret[$idx], 0, 1) == '"') { //If we started with a quote
if (substr($ret[$idx], -1) == '"') { //Are we ending in a quote?
$qts = substr_count($ret[$idx], '"') % 2; //Are there an even number of quotes?
if (!$qts) { //If there's an even amount of quotes, safe to close out this field
$ret[$idx] = trim($ret[$idx], '"'); //Remove the outer quotes
$ret[++$idx] = ''; //Start the next index
} else $ret[$idx].= ','; //Still inside a quoted field, don't ignore this comma, append it
} else $ret[$idx].= ','; //Still inside a quoted field, don't ignore this comma, append it
} else { //Non quoted field
$ret[++$idx] = ''; //Advance to next index
}
$lastpos = $pos+1; //Start our next search AFTER this comma
}
$ret[$idx].= substr($line, $lastpos); //Add whatever's after the last ,
$ret[$idx] = trim($ret[$idx], "\"\r\n"); //Remove any newlines/surrounding quotes
return $ret; //Return the array
}