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