Edit report at http://bugs.php.net/bug.php?id=53822&edit=1
ID: 53822 Updated by: cataphr...@php.net Reported by: withskyto at naver dot com Summary: fgetcsv parsing error -Status: Open +Status: Bogus Type: Bug Package: Filesystem function related Operating System: freebsd PHP Version: 5.2.17 Block user comment: N Private report: N New Comment: Use the last parameter to specify " as the escape character. <?php $str = <<<EOD A,B,C,D AAA,"BB,B","CCC,'\C,,CCC","D,DDD" "AA""AA","BB"",BBB""B","CC\""CC,,C""",DDD EOD; fwrite($fp = fopen('php://temp', 'r+'), $str); fseek($fp, 0, SEEK_SET); while ($arr = fgetcsv($fp, 10000, ',', '"', '"')) { print_r($arr); } gives the expected result. Previous Comments: ------------------------------------------------------------------------ [2011-01-24 03:27:45] withskyto at naver dot com I solve the problem as below. But the escape character's problem not solved yet. I save csv file from MS Office excel. The file looks as below. 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 "0-2","sample sample 2","10000","10000","10000","1","A","2000-12-15 10:46:22.802144","2000-12-15 11:46:22.801114","1000","10","C","I","ê°","ë","T" Test script: --------------- setlocale(LC_CTYPE, "ko_KR.UTF-8"); // not use ko_KR.eucKR $fp = fopen('test.csv', 'r'); while ($arr = fgetcsv($fp, 10000, ',', '"')) { print_r($arr); } Expected result: ---------------- ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 [11] => 11 [12] => 12 [13] => 13 [14] => 14 [15] => 15 ) Array ( [0] => 0-2 [1] => sample sample 2 [2] => 10000 [3] => 10000 [4] => 10000 [5] => 1 [6] => A [7] => 2000-12-15 10:46:22.802144 [8] => 2000-12-15 11:46:22.801114 [9] => 1000 [10] => 10 [11] => C [12] => I [13] => ê°ëë¤ [14] => ëë¤ë¼ [15] => T ) Actual result: -------------- Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 [11] => 11 [12] => 12 [13] => 13 [14] => 14 [15] => 15 ) Array ( [0] => 0-2 [1] => sample sample 2 [2] => 10000 [3] => 10000 [4] => 10000 [5] => 1 [6] => A [7] => 2000-12-15 10:46:22.802144 [8] => 2000-12-15 11:46:22.801114 [9] => 1000 [10] => 10 [11] => C [12] => I [13] => ê°ëë¤ [14] => ëë¤ë¼ [15] => T ) ------------------------------------------------------------------------ [2011-01-24 03:12:27] withskyto at naver dot com I save csv file from MS Office excel. The file looks as below. 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 "0-2","sample sample 2","10000","10000","10000","1","A","2000-12-15 10:46:22.802144","2000-12-15 11:46:22.801114","1000","10","C","I","ê°","ë","T" Test script: --------------- setlocale(LC_CTYPE, "ko_KR.eucKR"); $fp = fopen('test.csv', 'r'); while ($arr = fgetcsv($fp, 10000, ',', '"')) { print_r($arr); } Expected result: ---------------- Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 [11] => 11 [12] => 12 [13] => 13 [14] => 14 [15] => 15 ) Array ( [0] => 0-2 [1] => sample sample 2 [2] => 10000 [3] => 10000 [4] => 10000 [5] => 1 [6] => A [7] => 2000-12-15 10:46:22.802144 [8] => 2000-12-15 11:46:22.801114 [9] => 1000 [10] => 10 [11] => C [12] => I [13] => ê° [14] => ë [15] => T ) Actual result: -------------- Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 [11] => 11 [12] => 12 [13] => 13 [14] => 14 [15] => 15 ) Array ( [0] => 0-2 [1] => sample sample 2 [2] => 10000 [3] => 10000 [4] => 10000 [5] => 1 [6] => A [7] => 2000-12-15 10:46:22.802144 [8] => 2000-12-15 11:46:22.801114 [9] => 1000 [10] => 10 [11] => C [12] => I [13] => ê°",ë" [14] => T ) ------------------------------------------------------------------------ [2011-01-23 15:51:34] withskyto at naver dot com Change Status ------------------------------------------------------------------------ [2011-01-23 15:49:35] withskyto at naver dot com change Package ------------------------------------------------------------------------ [2011-01-23 15:09:21] withskyto at naver dot com I check this file. OpenOffice.org Calc and MS Offie excel - It seems to parse properly. ------------------------------------------------------------------------ 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://bugs.php.net/bug.php?id=53822 -- Edit this bug report at http://bugs.php.net/bug.php?id=53822&edit=1