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