note 40896 modified in function.fgetcsv by aidan

From: Date: Thu, 01 Jul 2004 01:01:08 +0000
Subject: note 40896 modified in function.fgetcsv by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-72207@lists.php.net to get a copy of this message
I found a way to parse CSVs with Perl RegExps and it's a lot faster and more efficient than the conventional fgetcsv() way. The fgetcsv() took more than 2 minutes to parse a 15,000 lined file while my function takes 6 to 7 secs on a Celeron 366 MHz prehistoric machine. Here's a link to the source of my function. fgetcsvfromline() source http://bu.orbitel.bg/fgetcsvfromline.php fgetcsvfromline() in action http://bu.orbitel.bg/csv.php Please let me know if you use it or if you've found a better solution. --was-- I'm not happy seing my contribution was deleted just because someone thought it's universal and should always work. It was obvious the function uses double quotes as a text delimiter and comma as a field delimiter - so it's even more obvious that double quotes in the text fields won't work. The moral I wanted to present was: "I found a way to parse CSVs with Perl RegExps and it's a lot faster and more efficient than the conventional fgetcsv() way" but not "Hey there, copy/paste the following code in your script and the banking software you're writing will work". Whatever - I'm here to help not to judge. By posting the function I was just letting you know what I learned from my practice with parsing CSVs and finding a solution better than the built-in fgetcsv() function. As I mentioned the fgetcsv() took more than 2 minutes to parse a 15,000 lined file while my function takes 6 to 7 secs on a Celeron 366 MHz prehistoric machine. As far as a lot of people wrote me about it I decided to make it abstract and post it here again. I hope deletion won't be done without letting me know about it. Here's a link to the source of my function (I don't paste it here because the publishing system says it has errors :-)). fgetcsvfromline() source : http://bu.orbitel.bg/fgetcsvfromline.php fgetcsvfromline() in action : http://bu.orbitel.bg/csv.php Here's the core source: $regExpSpecialChars = array ( "|" => "\\|", "&" => "\\&", "$" => "\\$", "(" => "\\(", ")" => "\\)", "^" => "\\^", "[" => "\\[", "]" => "\\]", "{" => "\\{", "}" => "\\}", "." => "\\.", "*" => "\\*", "\\" => "\\\\", "/" => "\\/" ); function fgetcsvfromline ($line, $columnCount, $delimiterChar = ',', $enclosureChar = '"') { global $regExpSpecialChars; $matches = array(); $delimiterChar = strtr($delimiterChar, $regExpSpecialChars); $enclosureChar = strtr($enclosureChar, $regExpSpecialChars); $regExp = "/^"; for ($i = 0; $i < $columnCount; $i++) { $regExp .= '('.$enclosureChar.'?)(.*)\\'.(2*$i + 1).$delimiterChar; } $regExp = substr($regExp, 0, (strlen($regExp) - strlen($delimiterChar)))."/"; if (preg_match($regExp, $line, $matches)) { return $matches; } return 0; } Please let me know if you use it and if you've found a better solution. http://php.net/manual/en/function.fgetcsv.php

« previous php.notes (#72207) next »