Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.6.0a3

Request #15684 Add str_getcsv support for 5.1.0 +
Submitted: 2009-01-20 00:34 UTC
From: doconnor Assigned: arpad
Status: Closed Package: PHP_Compat (version 1.6.0a2)
PHP Version: 5.2.6 OS:
Roadmaps: 1.6.0a3    
Subscription  


Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know! Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem : 31 + 36 = ?

 
 [2009-01-20 00:34 UTC] doconnor (Daniel O'Connor)
Description: ------------ No str_getcsv support afaict Test script: --------------- if (!function_exists('str_getcsv')) { function str_getcsv($input, $delimiter = ",", $enclosure = '"', $escape = "\\") { $fiveMBs = 5 * 1024 * 1024; $fp = fopen("php://temp/maxmemory:$fiveMBs", 'r+'); fputs($fp, $input); rewind($fp); $data = fgetcsv($fp, 1000, $delimiter, $enclosure); // $escape only got added in 5.3.0 fclose($fp); return $data; } }

Comments

 [2009-01-21 12:33 UTC] hm2k (James Wade)
Please test the attached patch, let me know if it functions as expected so it can be added into the CVS.
 [2009-05-07 01:37 UTC] arpad (Arpad Ray)
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-05-07 03:02 UTC] arpad (Arpad Ray)
-Assigned To: +Assigned To: arpad
 [2009-12-18 02:44 UTC] mckinzey (John 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-12-18 04:33 UTC] doconnor (Daniel O'Connor)
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?
 [2010-06-12 19:06 UTC] boris735 (Boris Jamieson)
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; } }
 [2010-08-01 19:49 UTC] hm2k (James Wade)
-Status: Assigned +Status: Closed
This bug has been fixed in SVN. If this was a documentation problem, the fix will appear on pear.php.net by the end of next Sunday (CET). If this was a problem with the pear.php.net website, the change should be live shortly. Otherwise, the fix will appear in the package's next release. Thank you for the report and for helping us make PEAR better.