note 40896 added to function.fgetcsv
| From: | bu at orbitel dot bg | Date: | Thu, 18 Mar 2004 13:54:45 +0000 |
| Subject: | note 40896 added to function.fgetcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-67016@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+40896
Delete -- http://master.php.net/manage/user-notes.php?action=delete+40896&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+40896&report=yes
Search -- http://master.php.net/manage/user-notes.php