1--TEST--
2Test fgetcsv() : usage variations - with default enclosure and different delimiter
3--FILE--
4<?php
5/*
6 Prototype: array fgetcsv ( resource $handle [, int $length [, string $delimiter [, string $enclosure]]] );
7 Description: Gets line from file pointer and parse for CSV fields
8*/
9
10/* Testing fgetcsv() to read a file when provided with default enclosure character
11   and with delimiter character which is not in the line being read by fgetcsv()
12*/
13
14echo "*** Testing fgetcsv() : with default enclosure and different delimiter ***\n";
15
16/* the array is with two elements in it. Each element should be read as
17   1st element is delimiter & 2nd element is csv fields
18*/
19$csv_lists = array (
20  array(',', 'water,fruit'),
21  array(' ', 'water fruit'),
22  array(' ', '"water" "fruit"'),
23  array('\\', 'water\\"fruit"\\"air"'),
24  array('\\', '"water"\\"fruit"\\"""'),
25);
26
27$filename = dirname(__FILE__) . '/fgetcsv_variation18.tmp';
28@unlink($filename);
29
30$file_modes = array ("r","rb", "rt", "r+", "r+b", "r+t",
31                     "a+", "a+b", "a+t",
32                     "w+", "w+b", "w+t",
33                     "x+", "x+b", "x+t");
34
35$loop_counter = 1;
36foreach ($csv_lists as $csv_list) {
37  for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) {
38    // create the file and add the content with has csv fields
39    if ( strstr($file_modes[$mode_counter], "r") ) {
40      $file_handle = fopen($filename, "w");
41    } else {
42      $file_handle = fopen($filename, $file_modes[$mode_counter] );
43    }
44    if ( !$file_handle ) {
45      echo "Error: failed to create file $filename!\n";
46      exit();
47    }
48    $delimiter = $csv_list[0];
49    $csv_field = $csv_list[1];
50    fwrite($file_handle, $csv_field . "\n");
51    // write another line of text and a blank line
52    // this will be used to test, if the fgetcsv() read more than a line and its
53    // working when only a blank line is read
54    fwrite($file_handle, "This is line of text without csv fields\n");
55    fwrite($file_handle, "\n"); // blank line
56
57    // close the file if the mode to be used is read mode  and re-open using read mode
58    // else rewind the file pointer to beginning of the file
59    if ( strstr($file_modes[$mode_counter], "r" ) ) {
60      fclose($file_handle);
61      $file_handle = fopen($filename, $file_modes[$mode_counter]);
62    } else {
63      // rewind the file pointer to bof
64      rewind($file_handle);
65    }
66
67    echo "\n-- Testing fgetcsv() with file opened using $file_modes[$mode_counter] mode --\n";
68
69    // call fgetcsv() to parse csv fields
70
71    // use different delimiter than existing in file
72    fseek($file_handle, 0, SEEK_SET);
73    $del = "+";
74    var_dump( fgetcsv($file_handle, 1024, $del) );
75    // check the file pointer position and if eof
76    var_dump( ftell($file_handle) );
77    var_dump( feof($file_handle) );
78
79    // close the file
80    fclose($file_handle);
81    //delete file
82    unlink($filename);
83  } //end of mode loop
84} // end of foreach
85
86echo "Done\n";
87?>
88--EXPECT--
89*** Testing fgetcsv() : with default enclosure and different delimiter ***
90
91-- Testing fgetcsv() with file opened using r mode --
92array(1) {
93  [0]=>
94  string(11) "water,fruit"
95}
96int(12)
97bool(false)
98
99-- Testing fgetcsv() with file opened using rb mode --
100array(1) {
101  [0]=>
102  string(11) "water,fruit"
103}
104int(12)
105bool(false)
106
107-- Testing fgetcsv() with file opened using rt mode --
108array(1) {
109  [0]=>
110  string(11) "water,fruit"
111}
112int(12)
113bool(false)
114
115-- Testing fgetcsv() with file opened using r+ mode --
116array(1) {
117  [0]=>
118  string(11) "water,fruit"
119}
120int(12)
121bool(false)
122
123-- Testing fgetcsv() with file opened using r+b mode --
124array(1) {
125  [0]=>
126  string(11) "water,fruit"
127}
128int(12)
129bool(false)
130
131-- Testing fgetcsv() with file opened using r+t mode --
132array(1) {
133  [0]=>
134  string(11) "water,fruit"
135}
136int(12)
137bool(false)
138
139-- Testing fgetcsv() with file opened using a+ mode --
140array(1) {
141  [0]=>
142  string(11) "water,fruit"
143}
144int(12)
145bool(false)
146
147-- Testing fgetcsv() with file opened using a+b mode --
148array(1) {
149  [0]=>
150  string(11) "water,fruit"
151}
152int(12)
153bool(false)
154
155-- Testing fgetcsv() with file opened using a+t mode --
156array(1) {
157  [0]=>
158  string(11) "water,fruit"
159}
160int(12)
161bool(false)
162
163-- Testing fgetcsv() with file opened using w+ mode --
164array(1) {
165  [0]=>
166  string(11) "water,fruit"
167}
168int(12)
169bool(false)
170
171-- Testing fgetcsv() with file opened using w+b mode --
172array(1) {
173  [0]=>
174  string(11) "water,fruit"
175}
176int(12)
177bool(false)
178
179-- Testing fgetcsv() with file opened using w+t mode --
180array(1) {
181  [0]=>
182  string(11) "water,fruit"
183}
184int(12)
185bool(false)
186
187-- Testing fgetcsv() with file opened using x+ mode --
188array(1) {
189  [0]=>
190  string(11) "water,fruit"
191}
192int(12)
193bool(false)
194
195-- Testing fgetcsv() with file opened using x+b mode --
196array(1) {
197  [0]=>
198  string(11) "water,fruit"
199}
200int(12)
201bool(false)
202
203-- Testing fgetcsv() with file opened using x+t mode --
204array(1) {
205  [0]=>
206  string(11) "water,fruit"
207}
208int(12)
209bool(false)
210
211-- Testing fgetcsv() with file opened using r mode --
212array(1) {
213  [0]=>
214  string(11) "water fruit"
215}
216int(12)
217bool(false)
218
219-- Testing fgetcsv() with file opened using rb mode --
220array(1) {
221  [0]=>
222  string(11) "water fruit"
223}
224int(12)
225bool(false)
226
227-- Testing fgetcsv() with file opened using rt mode --
228array(1) {
229  [0]=>
230  string(11) "water fruit"
231}
232int(12)
233bool(false)
234
235-- Testing fgetcsv() with file opened using r+ mode --
236array(1) {
237  [0]=>
238  string(11) "water fruit"
239}
240int(12)
241bool(false)
242
243-- Testing fgetcsv() with file opened using r+b mode --
244array(1) {
245  [0]=>
246  string(11) "water fruit"
247}
248int(12)
249bool(false)
250
251-- Testing fgetcsv() with file opened using r+t mode --
252array(1) {
253  [0]=>
254  string(11) "water fruit"
255}
256int(12)
257bool(false)
258
259-- Testing fgetcsv() with file opened using a+ mode --
260array(1) {
261  [0]=>
262  string(11) "water fruit"
263}
264int(12)
265bool(false)
266
267-- Testing fgetcsv() with file opened using a+b mode --
268array(1) {
269  [0]=>
270  string(11) "water fruit"
271}
272int(12)
273bool(false)
274
275-- Testing fgetcsv() with file opened using a+t mode --
276array(1) {
277  [0]=>
278  string(11) "water fruit"
279}
280int(12)
281bool(false)
282
283-- Testing fgetcsv() with file opened using w+ mode --
284array(1) {
285  [0]=>
286  string(11) "water fruit"
287}
288int(12)
289bool(false)
290
291-- Testing fgetcsv() with file opened using w+b mode --
292array(1) {
293  [0]=>
294  string(11) "water fruit"
295}
296int(12)
297bool(false)
298
299-- Testing fgetcsv() with file opened using w+t mode --
300array(1) {
301  [0]=>
302  string(11) "water fruit"
303}
304int(12)
305bool(false)
306
307-- Testing fgetcsv() with file opened using x+ mode --
308array(1) {
309  [0]=>
310  string(11) "water fruit"
311}
312int(12)
313bool(false)
314
315-- Testing fgetcsv() with file opened using x+b mode --
316array(1) {
317  [0]=>
318  string(11) "water fruit"
319}
320int(12)
321bool(false)
322
323-- Testing fgetcsv() with file opened using x+t mode --
324array(1) {
325  [0]=>
326  string(11) "water fruit"
327}
328int(12)
329bool(false)
330
331-- Testing fgetcsv() with file opened using r mode --
332array(1) {
333  [0]=>
334  string(13) "water "fruit""
335}
336int(16)
337bool(false)
338
339-- Testing fgetcsv() with file opened using rb mode --
340array(1) {
341  [0]=>
342  string(13) "water "fruit""
343}
344int(16)
345bool(false)
346
347-- Testing fgetcsv() with file opened using rt mode --
348array(1) {
349  [0]=>
350  string(13) "water "fruit""
351}
352int(16)
353bool(false)
354
355-- Testing fgetcsv() with file opened using r+ mode --
356array(1) {
357  [0]=>
358  string(13) "water "fruit""
359}
360int(16)
361bool(false)
362
363-- Testing fgetcsv() with file opened using r+b mode --
364array(1) {
365  [0]=>
366  string(13) "water "fruit""
367}
368int(16)
369bool(false)
370
371-- Testing fgetcsv() with file opened using r+t mode --
372array(1) {
373  [0]=>
374  string(13) "water "fruit""
375}
376int(16)
377bool(false)
378
379-- Testing fgetcsv() with file opened using a+ mode --
380array(1) {
381  [0]=>
382  string(13) "water "fruit""
383}
384int(16)
385bool(false)
386
387-- Testing fgetcsv() with file opened using a+b mode --
388array(1) {
389  [0]=>
390  string(13) "water "fruit""
391}
392int(16)
393bool(false)
394
395-- Testing fgetcsv() with file opened using a+t mode --
396array(1) {
397  [0]=>
398  string(13) "water "fruit""
399}
400int(16)
401bool(false)
402
403-- Testing fgetcsv() with file opened using w+ mode --
404array(1) {
405  [0]=>
406  string(13) "water "fruit""
407}
408int(16)
409bool(false)
410
411-- Testing fgetcsv() with file opened using w+b mode --
412array(1) {
413  [0]=>
414  string(13) "water "fruit""
415}
416int(16)
417bool(false)
418
419-- Testing fgetcsv() with file opened using w+t mode --
420array(1) {
421  [0]=>
422  string(13) "water "fruit""
423}
424int(16)
425bool(false)
426
427-- Testing fgetcsv() with file opened using x+ mode --
428array(1) {
429  [0]=>
430  string(13) "water "fruit""
431}
432int(16)
433bool(false)
434
435-- Testing fgetcsv() with file opened using x+b mode --
436array(1) {
437  [0]=>
438  string(13) "water "fruit""
439}
440int(16)
441bool(false)
442
443-- Testing fgetcsv() with file opened using x+t mode --
444array(1) {
445  [0]=>
446  string(13) "water "fruit""
447}
448int(16)
449bool(false)
450
451-- Testing fgetcsv() with file opened using r mode --
452array(1) {
453  [0]=>
454  string(19) "water\"fruit"\"air""
455}
456int(20)
457bool(false)
458
459-- Testing fgetcsv() with file opened using rb mode --
460array(1) {
461  [0]=>
462  string(19) "water\"fruit"\"air""
463}
464int(20)
465bool(false)
466
467-- Testing fgetcsv() with file opened using rt mode --
468array(1) {
469  [0]=>
470  string(19) "water\"fruit"\"air""
471}
472int(20)
473bool(false)
474
475-- Testing fgetcsv() with file opened using r+ mode --
476array(1) {
477  [0]=>
478  string(19) "water\"fruit"\"air""
479}
480int(20)
481bool(false)
482
483-- Testing fgetcsv() with file opened using r+b mode --
484array(1) {
485  [0]=>
486  string(19) "water\"fruit"\"air""
487}
488int(20)
489bool(false)
490
491-- Testing fgetcsv() with file opened using r+t mode --
492array(1) {
493  [0]=>
494  string(19) "water\"fruit"\"air""
495}
496int(20)
497bool(false)
498
499-- Testing fgetcsv() with file opened using a+ mode --
500array(1) {
501  [0]=>
502  string(19) "water\"fruit"\"air""
503}
504int(20)
505bool(false)
506
507-- Testing fgetcsv() with file opened using a+b mode --
508array(1) {
509  [0]=>
510  string(19) "water\"fruit"\"air""
511}
512int(20)
513bool(false)
514
515-- Testing fgetcsv() with file opened using a+t mode --
516array(1) {
517  [0]=>
518  string(19) "water\"fruit"\"air""
519}
520int(20)
521bool(false)
522
523-- Testing fgetcsv() with file opened using w+ mode --
524array(1) {
525  [0]=>
526  string(19) "water\"fruit"\"air""
527}
528int(20)
529bool(false)
530
531-- Testing fgetcsv() with file opened using w+b mode --
532array(1) {
533  [0]=>
534  string(19) "water\"fruit"\"air""
535}
536int(20)
537bool(false)
538
539-- Testing fgetcsv() with file opened using w+t mode --
540array(1) {
541  [0]=>
542  string(19) "water\"fruit"\"air""
543}
544int(20)
545bool(false)
546
547-- Testing fgetcsv() with file opened using x+ mode --
548array(1) {
549  [0]=>
550  string(19) "water\"fruit"\"air""
551}
552int(20)
553bool(false)
554
555-- Testing fgetcsv() with file opened using x+b mode --
556array(1) {
557  [0]=>
558  string(19) "water\"fruit"\"air""
559}
560int(20)
561bool(false)
562
563-- Testing fgetcsv() with file opened using x+t mode --
564array(1) {
565  [0]=>
566  string(19) "water\"fruit"\"air""
567}
568int(20)
569bool(false)
570
571-- Testing fgetcsv() with file opened using r mode --
572array(1) {
573  [0]=>
574  string(17) "water\"fruit"\""""
575}
576int(20)
577bool(false)
578
579-- Testing fgetcsv() with file opened using rb mode --
580array(1) {
581  [0]=>
582  string(17) "water\"fruit"\""""
583}
584int(20)
585bool(false)
586
587-- Testing fgetcsv() with file opened using rt mode --
588array(1) {
589  [0]=>
590  string(17) "water\"fruit"\""""
591}
592int(20)
593bool(false)
594
595-- Testing fgetcsv() with file opened using r+ mode --
596array(1) {
597  [0]=>
598  string(17) "water\"fruit"\""""
599}
600int(20)
601bool(false)
602
603-- Testing fgetcsv() with file opened using r+b mode --
604array(1) {
605  [0]=>
606  string(17) "water\"fruit"\""""
607}
608int(20)
609bool(false)
610
611-- Testing fgetcsv() with file opened using r+t mode --
612array(1) {
613  [0]=>
614  string(17) "water\"fruit"\""""
615}
616int(20)
617bool(false)
618
619-- Testing fgetcsv() with file opened using a+ mode --
620array(1) {
621  [0]=>
622  string(17) "water\"fruit"\""""
623}
624int(20)
625bool(false)
626
627-- Testing fgetcsv() with file opened using a+b mode --
628array(1) {
629  [0]=>
630  string(17) "water\"fruit"\""""
631}
632int(20)
633bool(false)
634
635-- Testing fgetcsv() with file opened using a+t mode --
636array(1) {
637  [0]=>
638  string(17) "water\"fruit"\""""
639}
640int(20)
641bool(false)
642
643-- Testing fgetcsv() with file opened using w+ mode --
644array(1) {
645  [0]=>
646  string(17) "water\"fruit"\""""
647}
648int(20)
649bool(false)
650
651-- Testing fgetcsv() with file opened using w+b mode --
652array(1) {
653  [0]=>
654  string(17) "water\"fruit"\""""
655}
656int(20)
657bool(false)
658
659-- Testing fgetcsv() with file opened using w+t mode --
660array(1) {
661  [0]=>
662  string(17) "water\"fruit"\""""
663}
664int(20)
665bool(false)
666
667-- Testing fgetcsv() with file opened using x+ mode --
668array(1) {
669  [0]=>
670  string(17) "water\"fruit"\""""
671}
672int(20)
673bool(false)
674
675-- Testing fgetcsv() with file opened using x+b mode --
676array(1) {
677  [0]=>
678  string(17) "water\"fruit"\""""
679}
680int(20)
681bool(false)
682
683-- Testing fgetcsv() with file opened using x+t mode --
684array(1) {
685  [0]=>
686  string(17) "water\"fruit"\""""
687}
688int(20)
689bool(false)
690Done
691