xref: /PHP-8.4/ext/standard/tests/file/fputcsv.phpt (revision f756b96e)
1--TEST--
2various fputcsv() functionality tests
3--FILE--
4<?php
5
6$list = array (
7  0 => 'aaa,bbb',
8  1 => 'aaa,"bbb"',
9  2 => '"aaa","bbb"',
10  3 => 'aaa,bbb',
11  4 => '"aaa",bbb',
12  5 => '"aaa",   "bbb"',
13  6 => ',',
14  7 => 'aaa,',
15  8 => ',"aaa"',
16  9 => '"",""',
17  10 => '"""""",',
18  11 => '""""",aaa',
19  12 => 'aaa,bbb   ',
20  13 => 'aaa,"bbb   "',
21  14 => 'aaa"aaa","bbb"bbb',
22  15 => 'aaa"aaa""",bbb',
23  16 => 'aaa,"\\"bbb,ccc',
24  17 => 'aaa"\\"a","bbb"',
25  18 => '"\\"","aaa"',
26  19 => '"\\""",aaa',
27);
28
29$file = __DIR__ . '/fputcsv.csv';
30@unlink($file);
31
32$fp = fopen($file, "w");
33foreach ($list as $v) {
34    fputcsv($fp, explode(',', $v), escape: "\\");
35}
36fclose($fp);
37
38$res = file($file);
39foreach($res as &$val)
40{
41    $val = substr($val, 0, -1);
42}
43echo '$list = ';var_export($res);echo ";\n";
44
45$fp = fopen($file, "r");
46$res = array();
47while($l=fgetcsv($fp, escape: "\\"))
48{
49    $res[] = join(',',$l);
50}
51fclose($fp);
52
53echo '$list = ';var_export($res);echo ";\n";
54
55@unlink($file);
56
57?>
58--EXPECT--
59$list = array (
60  0 => 'aaa,bbb',
61  1 => 'aaa,"""bbb"""',
62  2 => '"""aaa""","""bbb"""',
63  3 => 'aaa,bbb',
64  4 => '"""aaa""",bbb',
65  5 => '"""aaa""","   ""bbb"""',
66  6 => ',',
67  7 => 'aaa,',
68  8 => ',"""aaa"""',
69  9 => '"""""",""""""',
70  10 => '"""""""""""""",',
71  11 => '"""""""""""",aaa',
72  12 => 'aaa,"bbb   "',
73  13 => 'aaa,"""bbb   """',
74  14 => '"aaa""aaa""","""bbb""bbb"',
75  15 => '"aaa""aaa""""""",bbb',
76  16 => 'aaa,"""\\"bbb",ccc',
77  17 => '"aaa""\\"a""","""bbb"""',
78  18 => '"""\\"""","""aaa"""',
79  19 => '"""\\"""""",aaa',
80);
81$list = array (
82  0 => 'aaa,bbb',
83  1 => 'aaa,"bbb"',
84  2 => '"aaa","bbb"',
85  3 => 'aaa,bbb',
86  4 => '"aaa",bbb',
87  5 => '"aaa",   "bbb"',
88  6 => ',',
89  7 => 'aaa,',
90  8 => ',"aaa"',
91  9 => '"",""',
92  10 => '"""""",',
93  11 => '""""",aaa',
94  12 => 'aaa,bbb   ',
95  13 => 'aaa,"bbb   "',
96  14 => 'aaa"aaa","bbb"bbb',
97  15 => 'aaa"aaa""",bbb',
98  16 => 'aaa,"\\"bbb,ccc',
99  17 => 'aaa"\\"a","bbb"',
100  18 => '"\\"","aaa"',
101  19 => '"\\""",aaa',
102);
103