1--TEST--
2Test fputcsv() : usage variations - with different delimiter and same enclosure
3--FILE--
4<?php
5
6/*
7   Testing fputcsv() to write to a file when enclosure is same but delimiter is different from those
8   present in the field to be written to the file
9 */
10
11echo "*** Testing fputcsv() : with different delimiter and same enclosure ***\n";
12
13/* the array is with three elements in it. Each element should be read as
14   1st element is delimiter, 2nd element is enclosure
15   and 3rd element is csv fields
16*/
17$csv_lists = array (
18  array(',', '"', array('water','fruit') ),
19  array(',', '"', array('"water","fruit') ),
20  array(',', '"', array('"water","fruit"') ),
21  array(' ', '^', array('^water^ ^fruit^')),
22  array(':', '&', array('&water&:&fruit&')),
23  array('=', '=', array('=water===fruit=')),
24  array('-', '-', array('-water--fruit-air')),
25  array('-', '-', array('-water---fruit---air-')),
26  array(':', '&', array('&""""&:&"&:,:":&,&:,,,,'))
27
28);
29$file_path = __DIR__;
30$file = "$file_path/fputcsv_variation7.tmp";
31
32$file_modes = array ("r+", "r+b", "r+t",
33                     "a+", "a+b", "a+t",
34                     "w+", "w+b", "w+t",
35                     "x+", "x+b", "x+t");
36
37$loop_counter = 1;
38foreach ($csv_lists as $csv_list) {
39  for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) {
40
41    echo "\n-- file opened in $file_modes[$mode_counter] --\n";
42    // create the file and add the content with has csv fields
43    if ( strstr($file_modes[$mode_counter], "r") ) {
44      $fo = new SplFileObject($file, 'w');
45    } else {
46      $fo = new SplFileObject($file, $file_modes[$mode_counter]);
47    }
48    $delimiter = $csv_list[0];
49    $enclosure = $csv_list[1];
50    $csv_field = $csv_list[2];
51
52    // write to a file in csv format
53    var_dump( $fo->fputcsv($csv_field, '+', $enclosure) );
54    // check the file pointer position and eof
55    var_dump( $fo->ftell() );
56    var_dump( $fo->eof() );
57    //close the file
58    unset($fo);
59
60    // print the file contents
61    var_dump( file_get_contents($file) );
62
63    //delete file
64    unlink($file);
65  } //end of mode loop
66} // end of foreach
67
68echo "Done\n";
69?>
70--EXPECTF--
71*** Testing fputcsv() : with different delimiter and same enclosure ***
72
73-- file opened in r+ --
74int(12)
75int(12)
76bool(false)
77string(12) "water+fruit
78"
79
80-- file opened in r+b --
81int(12)
82int(12)
83bool(false)
84string(12) "water+fruit
85"
86
87-- file opened in r+t --
88int(12)
89int(12)
90bool(false)
91string(%d) "water+fruit
92"
93
94-- file opened in a+ --
95int(12)
96int(12)
97bool(false)
98string(12) "water+fruit
99"
100
101-- file opened in a+b --
102int(12)
103int(12)
104bool(false)
105string(12) "water+fruit
106"
107
108-- file opened in a+t --
109int(12)
110int(12)
111bool(false)
112string(%d) "water+fruit
113"
114
115-- file opened in w+ --
116int(12)
117int(12)
118bool(false)
119string(12) "water+fruit
120"
121
122-- file opened in w+b --
123int(12)
124int(12)
125bool(false)
126string(12) "water+fruit
127"
128
129-- file opened in w+t --
130int(12)
131int(12)
132bool(false)
133string(%d) "water+fruit
134"
135
136-- file opened in x+ --
137int(12)
138int(12)
139bool(false)
140string(12) "water+fruit
141"
142
143-- file opened in x+b --
144int(12)
145int(12)
146bool(false)
147string(12) "water+fruit
148"
149
150-- file opened in x+t --
151int(12)
152int(12)
153bool(false)
154string(%d) "water+fruit
155"
156
157-- file opened in r+ --
158int(20)
159int(20)
160bool(false)
161string(20) """"water"",""fruit"
162"
163
164-- file opened in r+b --
165int(20)
166int(20)
167bool(false)
168string(20) """"water"",""fruit"
169"
170
171-- file opened in r+t --
172int(20)
173int(20)
174bool(false)
175string(%d) """"water"",""fruit"
176"
177
178-- file opened in a+ --
179int(20)
180int(20)
181bool(false)
182string(20) """"water"",""fruit"
183"
184
185-- file opened in a+b --
186int(20)
187int(20)
188bool(false)
189string(20) """"water"",""fruit"
190"
191
192-- file opened in a+t --
193int(20)
194int(20)
195bool(false)
196string(%d) """"water"",""fruit"
197"
198
199-- file opened in w+ --
200int(20)
201int(20)
202bool(false)
203string(20) """"water"",""fruit"
204"
205
206-- file opened in w+b --
207int(20)
208int(20)
209bool(false)
210string(20) """"water"",""fruit"
211"
212
213-- file opened in w+t --
214int(20)
215int(20)
216bool(false)
217string(%d) """"water"",""fruit"
218"
219
220-- file opened in x+ --
221int(20)
222int(20)
223bool(false)
224string(20) """"water"",""fruit"
225"
226
227-- file opened in x+b --
228int(20)
229int(20)
230bool(false)
231string(20) """"water"",""fruit"
232"
233
234-- file opened in x+t --
235int(20)
236int(20)
237bool(false)
238string(%d) """"water"",""fruit"
239"
240
241-- file opened in r+ --
242int(22)
243int(22)
244bool(false)
245string(22) """"water"",""fruit"""
246"
247
248-- file opened in r+b --
249int(22)
250int(22)
251bool(false)
252string(22) """"water"",""fruit"""
253"
254
255-- file opened in r+t --
256int(22)
257int(22)
258bool(false)
259string(%d) """"water"",""fruit"""
260"
261
262-- file opened in a+ --
263int(22)
264int(22)
265bool(false)
266string(22) """"water"",""fruit"""
267"
268
269-- file opened in a+b --
270int(22)
271int(22)
272bool(false)
273string(22) """"water"",""fruit"""
274"
275
276-- file opened in a+t --
277int(22)
278int(22)
279bool(false)
280string(%d) """"water"",""fruit"""
281"
282
283-- file opened in w+ --
284int(22)
285int(22)
286bool(false)
287string(22) """"water"",""fruit"""
288"
289
290-- file opened in w+b --
291int(22)
292int(22)
293bool(false)
294string(22) """"water"",""fruit"""
295"
296
297-- file opened in w+t --
298int(22)
299int(22)
300bool(false)
301string(%d) """"water"",""fruit"""
302"
303
304-- file opened in x+ --
305int(22)
306int(22)
307bool(false)
308string(22) """"water"",""fruit"""
309"
310
311-- file opened in x+b --
312int(22)
313int(22)
314bool(false)
315string(22) """"water"",""fruit"""
316"
317
318-- file opened in x+t --
319int(22)
320int(22)
321bool(false)
322string(%d) """"water"",""fruit"""
323"
324
325-- file opened in r+ --
326int(22)
327int(22)
328bool(false)
329string(22) "^^^water^^ ^^fruit^^^
330"
331
332-- file opened in r+b --
333int(22)
334int(22)
335bool(false)
336string(22) "^^^water^^ ^^fruit^^^
337"
338
339-- file opened in r+t --
340int(22)
341int(22)
342bool(false)
343string(%d) "^^^water^^ ^^fruit^^^
344"
345
346-- file opened in a+ --
347int(22)
348int(22)
349bool(false)
350string(22) "^^^water^^ ^^fruit^^^
351"
352
353-- file opened in a+b --
354int(22)
355int(22)
356bool(false)
357string(22) "^^^water^^ ^^fruit^^^
358"
359
360-- file opened in a+t --
361int(22)
362int(22)
363bool(false)
364string(%d) "^^^water^^ ^^fruit^^^
365"
366
367-- file opened in w+ --
368int(22)
369int(22)
370bool(false)
371string(22) "^^^water^^ ^^fruit^^^
372"
373
374-- file opened in w+b --
375int(22)
376int(22)
377bool(false)
378string(22) "^^^water^^ ^^fruit^^^
379"
380
381-- file opened in w+t --
382int(22)
383int(22)
384bool(false)
385string(%d) "^^^water^^ ^^fruit^^^
386"
387
388-- file opened in x+ --
389int(22)
390int(22)
391bool(false)
392string(22) "^^^water^^ ^^fruit^^^
393"
394
395-- file opened in x+b --
396int(22)
397int(22)
398bool(false)
399string(22) "^^^water^^ ^^fruit^^^
400"
401
402-- file opened in x+t --
403int(22)
404int(22)
405bool(false)
406string(%d) "^^^water^^ ^^fruit^^^
407"
408
409-- file opened in r+ --
410int(22)
411int(22)
412bool(false)
413string(22) "&&&water&&:&&fruit&&&
414"
415
416-- file opened in r+b --
417int(22)
418int(22)
419bool(false)
420string(22) "&&&water&&:&&fruit&&&
421"
422
423-- file opened in r+t --
424int(22)
425int(22)
426bool(false)
427string(%d) "&&&water&&:&&fruit&&&
428"
429
430-- file opened in a+ --
431int(22)
432int(22)
433bool(false)
434string(22) "&&&water&&:&&fruit&&&
435"
436
437-- file opened in a+b --
438int(22)
439int(22)
440bool(false)
441string(22) "&&&water&&:&&fruit&&&
442"
443
444-- file opened in a+t --
445int(22)
446int(22)
447bool(false)
448string(%d) "&&&water&&:&&fruit&&&
449"
450
451-- file opened in w+ --
452int(22)
453int(22)
454bool(false)
455string(22) "&&&water&&:&&fruit&&&
456"
457
458-- file opened in w+b --
459int(22)
460int(22)
461bool(false)
462string(22) "&&&water&&:&&fruit&&&
463"
464
465-- file opened in w+t --
466int(22)
467int(22)
468bool(false)
469string(%d) "&&&water&&:&&fruit&&&
470"
471
472-- file opened in x+ --
473int(22)
474int(22)
475bool(false)
476string(22) "&&&water&&:&&fruit&&&
477"
478
479-- file opened in x+b --
480int(22)
481int(22)
482bool(false)
483string(22) "&&&water&&:&&fruit&&&
484"
485
486-- file opened in x+t --
487int(22)
488int(22)
489bool(false)
490string(%d) "&&&water&&:&&fruit&&&
491"
492
493-- file opened in r+ --
494int(23)
495int(23)
496bool(false)
497string(23) "===water======fruit===
498"
499
500-- file opened in r+b --
501int(23)
502int(23)
503bool(false)
504string(23) "===water======fruit===
505"
506
507-- file opened in r+t --
508int(23)
509int(23)
510bool(false)
511string(%d) "===water======fruit===
512"
513
514-- file opened in a+ --
515int(23)
516int(23)
517bool(false)
518string(23) "===water======fruit===
519"
520
521-- file opened in a+b --
522int(23)
523int(23)
524bool(false)
525string(23) "===water======fruit===
526"
527
528-- file opened in a+t --
529int(23)
530int(23)
531bool(false)
532string(%d) "===water======fruit===
533"
534
535-- file opened in w+ --
536int(23)
537int(23)
538bool(false)
539string(23) "===water======fruit===
540"
541
542-- file opened in w+b --
543int(23)
544int(23)
545bool(false)
546string(23) "===water======fruit===
547"
548
549-- file opened in w+t --
550int(23)
551int(23)
552bool(false)
553string(%d) "===water======fruit===
554"
555
556-- file opened in x+ --
557int(23)
558int(23)
559bool(false)
560string(23) "===water======fruit===
561"
562
563-- file opened in x+b --
564int(23)
565int(23)
566bool(false)
567string(23) "===water======fruit===
568"
569
570-- file opened in x+t --
571int(23)
572int(23)
573bool(false)
574string(%d) "===water======fruit===
575"
576
577-- file opened in r+ --
578int(24)
579int(24)
580bool(false)
581string(24) "---water----fruit--air-
582"
583
584-- file opened in r+b --
585int(24)
586int(24)
587bool(false)
588string(24) "---water----fruit--air-
589"
590
591-- file opened in r+t --
592int(24)
593int(24)
594bool(false)
595string(%d) "---water----fruit--air-
596"
597
598-- file opened in a+ --
599int(24)
600int(24)
601bool(false)
602string(24) "---water----fruit--air-
603"
604
605-- file opened in a+b --
606int(24)
607int(24)
608bool(false)
609string(24) "---water----fruit--air-
610"
611
612-- file opened in a+t --
613int(24)
614int(24)
615bool(false)
616string(%d) "---water----fruit--air-
617"
618
619-- file opened in w+ --
620int(24)
621int(24)
622bool(false)
623string(24) "---water----fruit--air-
624"
625
626-- file opened in w+b --
627int(24)
628int(24)
629bool(false)
630string(24) "---water----fruit--air-
631"
632
633-- file opened in w+t --
634int(24)
635int(24)
636bool(false)
637string(%d) "---water----fruit--air-
638"
639
640-- file opened in x+ --
641int(24)
642int(24)
643bool(false)
644string(24) "---water----fruit--air-
645"
646
647-- file opened in x+b --
648int(24)
649int(24)
650bool(false)
651string(24) "---water----fruit--air-
652"
653
654-- file opened in x+t --
655int(24)
656int(24)
657bool(false)
658string(%d) "---water----fruit--air-
659"
660
661-- file opened in r+ --
662int(32)
663int(32)
664bool(false)
665string(32) "---water------fruit------air---
666"
667
668-- file opened in r+b --
669int(32)
670int(32)
671bool(false)
672string(32) "---water------fruit------air---
673"
674
675-- file opened in r+t --
676int(32)
677int(32)
678bool(false)
679string(%d) "---water------fruit------air---
680"
681
682-- file opened in a+ --
683int(32)
684int(32)
685bool(false)
686string(32) "---water------fruit------air---
687"
688
689-- file opened in a+b --
690int(32)
691int(32)
692bool(false)
693string(32) "---water------fruit------air---
694"
695
696-- file opened in a+t --
697int(32)
698int(32)
699bool(false)
700string(%d) "---water------fruit------air---
701"
702
703-- file opened in w+ --
704int(32)
705int(32)
706bool(false)
707string(32) "---water------fruit------air---
708"
709
710-- file opened in w+b --
711int(32)
712int(32)
713bool(false)
714string(32) "---water------fruit------air---
715"
716
717-- file opened in w+t --
718int(32)
719int(32)
720bool(false)
721string(%d) "---water------fruit------air---
722"
723
724-- file opened in x+ --
725int(32)
726int(32)
727bool(false)
728string(32) "---water------fruit------air---
729"
730
731-- file opened in x+b --
732int(32)
733int(32)
734bool(false)
735string(32) "---water------fruit------air---
736"
737
738-- file opened in x+t --
739int(32)
740int(32)
741bool(false)
742string(%d) "---water------fruit------air---
743"
744
745-- file opened in r+ --
746int(32)
747int(32)
748bool(false)
749string(32) "&&&""""&&:&&"&&:,:":&&,&&:,,,,&
750"
751
752-- file opened in r+b --
753int(32)
754int(32)
755bool(false)
756string(32) "&&&""""&&:&&"&&:,:":&&,&&:,,,,&
757"
758
759-- file opened in r+t --
760int(32)
761int(32)
762bool(false)
763string(%d) "&&&""""&&:&&"&&:,:":&&,&&:,,,,&
764"
765
766-- file opened in a+ --
767int(32)
768int(32)
769bool(false)
770string(32) "&&&""""&&:&&"&&:,:":&&,&&:,,,,&
771"
772
773-- file opened in a+b --
774int(32)
775int(32)
776bool(false)
777string(32) "&&&""""&&:&&"&&:,:":&&,&&:,,,,&
778"
779
780-- file opened in a+t --
781int(32)
782int(32)
783bool(false)
784string(%d) "&&&""""&&:&&"&&:,:":&&,&&:,,,,&
785"
786
787-- file opened in w+ --
788int(32)
789int(32)
790bool(false)
791string(32) "&&&""""&&:&&"&&:,:":&&,&&:,,,,&
792"
793
794-- file opened in w+b --
795int(32)
796int(32)
797bool(false)
798string(32) "&&&""""&&:&&"&&:,:":&&,&&:,,,,&
799"
800
801-- file opened in w+t --
802int(32)
803int(32)
804bool(false)
805string(%d) "&&&""""&&:&&"&&:,:":&&,&&:,,,,&
806"
807
808-- file opened in x+ --
809int(32)
810int(32)
811bool(false)
812string(32) "&&&""""&&:&&"&&:,:":&&,&&:,,,,&
813"
814
815-- file opened in x+b --
816int(32)
817int(32)
818bool(false)
819string(32) "&&&""""&&:&&"&&:,:":&&,&&:,,,,&
820"
821
822-- file opened in x+t --
823int(32)
824int(32)
825bool(false)
826string(%d) "&&&""""&&:&&"&&:,:":&&,&&:,,,,&
827"
828Done
829