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