Req #15684 [Com]: Add str_getcsv support for 5.1.0 +
| From: | boris735 | Date: | Sat, 12 Jun 2010 15:08:54 +0000 |
| Subject: | Req #15684 [Com]: Add str_getcsv support for 5.1.0 + | ||
| References: | 1 | Groups: | php.pear.bugs |
| Request: | Send a blank email to pear-bugs+get-3926@lists.php.net to get a copy of this message | ||
Edit report at http://pear.php.net/bugs/bug.php?id=15684&edit=1
ID: 15684
Comment by: boris735
Reported By: boris735 at yahoo dot com dot au
Summary: Add str_getcsv support for 5.1.0 +
Status: Assigned
Type: Feature/Change Request
Package: PHP_Compat
Package Version: 1.6.0a2
PHP Version: 5.2.6
Assigned To: arpad
New Comment:
I just recently wrote a version of this that is PHP4 compatible. It
could probably use some error handling, but should serve as a useful
springboard for somebody to work with. Hope this helps someone.
---------------
if (!function_exists("str_getcsv")) {
function str_getcsv($input, $delimiter=',', $enclosure='"',
$escape='\\') {
$bs = '\\';
$enc = $bs . $enclosure;
$esc = $bs . $escape;
$delim = $bs . $delimiter;
$encesc = ($enc == $esc) ? $enc : $enc . $esc;
$pattern =
"/($enc(?:[^$encesc]|$esc$enc)*$enc|[^$enc$delim]*)$delim/";
preg_match_all($pattern, $input . $delimiter, $matches);
$parts = array();
foreach ($matches[1] as $part) {
$len = strlen($part);
if ($len >= 2 && $part{0} == $enclosure) {
$part = substr($part, 1, $len - 2);
$part = str_replace($escape . $enclosure, $enclosure, $part);
}
$parts[] = $part;
}
return $parts;
}
}
Previous Comments:
------------------------------------------------------------------------
[2009-12-17 23:33:50] doconnor
Pity :(
I don't suppose pre-escaping (str_replace("\r\n",
"ESCAPE_WORKAROUND_POORLY_NAMED_CONSTANT") kinda thing), writing to
stream, then unescaping would cut it?
------------------------------------------------------------------------
[2009-12-17 21:44:38] mckinzey
Daniel's function doesn't work on all inputs as str_getcsv should. In
particular,
<?php
print_r(str_getcsv("a,b,c\r\na,b,c"));
?>
On >= PHP 5.3.0, that'll yield the following:
Array
(
[0] => a
[1] => b
[2] => c
a
[3] => b
[4] => c
)
On < PHP 5.3.0 that'll yield this:
Array
(
[0] => a
[1] => b
[2] => c
)
Since, as far as I know, there's no way to get the line endings with
fgetcsv, using stream wrappers to make fgetcsv emulate str_getcsv is not
possible.
------------------------------------------------------------------------
[2009-05-06 23:02:44] arpad
<div id="changeset">
<span class="removed">-Assigned To:</span>
<span class="added">+Assigned To: arpad</span>
</div>
------------------------------------------------------------------------
[2009-05-06 21:37:14] arpad
James, the first argument for str_getcsv() is the actual input, not a
filename, so the patch is fundamentally flawed. Unit tests help too ;)
Daniel's function looks ok but php://temp gives us a dependency on PHP
>= 5.1.0. It would be nice (and doesn't seem inordinately difficult) to
make it PHP4 friendly.
------------------------------------------------------------------------
[2009-01-21 07:33:16] hm2k
Please test the attached patch, let me know if it functions as expected
so it can be added into the CVS.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://pear.php.net/bugs/bug.php?id=15684
--
Edit this bug report at http://pear.php.net/bugs/bug.php?id=15684&edit=1