![]() ![]() | ![]() |
1.1 misho 1: --TEST--
2: Test fgetcsv() : usage variations - with default enclosure & length as 0
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 length value equal to zero
12: */
13:
14: echo "*** Testing fgetcsv() : with default enclosure & length as 0 ***\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_variation16.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;
36: foreach ($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 blan 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
1.1.1.2 ! misho 58: // else rewind the file pointer to beginning of the file
1.1 misho 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 length as 0
72: fseek($file_handle, 0, SEEK_SET);
73: var_dump( fgetcsv($file_handle, 0, $delimiter) );
74: // check the file pointer position and if eof
75: var_dump( ftell($file_handle) );
76: var_dump( feof($file_handle) );
77:
78: // close the file
79: fclose($file_handle);
80: //delete file
81: unlink($filename);
82: } //end of mode loop
83: } // end of foreach
84:
85: echo "Done\n";
86: ?>
87: --EXPECT--
88: *** Testing fgetcsv() : with default enclosure & length as 0 ***
89:
90: -- Testing fgetcsv() with file opened using r mode --
91: array(2) {
92: [0]=>
93: string(5) "water"
94: [1]=>
95: string(5) "fruit"
96: }
97: int(12)
98: bool(false)
99:
100: -- Testing fgetcsv() with file opened using rb mode --
101: array(2) {
102: [0]=>
103: string(5) "water"
104: [1]=>
105: string(5) "fruit"
106: }
107: int(12)
108: bool(false)
109:
110: -- Testing fgetcsv() with file opened using rt mode --
111: array(2) {
112: [0]=>
113: string(5) "water"
114: [1]=>
115: string(5) "fruit"
116: }
117: int(12)
118: bool(false)
119:
120: -- Testing fgetcsv() with file opened using r+ mode --
121: array(2) {
122: [0]=>
123: string(5) "water"
124: [1]=>
125: string(5) "fruit"
126: }
127: int(12)
128: bool(false)
129:
130: -- Testing fgetcsv() with file opened using r+b mode --
131: array(2) {
132: [0]=>
133: string(5) "water"
134: [1]=>
135: string(5) "fruit"
136: }
137: int(12)
138: bool(false)
139:
140: -- Testing fgetcsv() with file opened using r+t mode --
141: array(2) {
142: [0]=>
143: string(5) "water"
144: [1]=>
145: string(5) "fruit"
146: }
147: int(12)
148: bool(false)
149:
150: -- Testing fgetcsv() with file opened using a+ mode --
151: array(2) {
152: [0]=>
153: string(5) "water"
154: [1]=>
155: string(5) "fruit"
156: }
157: int(12)
158: bool(false)
159:
160: -- Testing fgetcsv() with file opened using a+b mode --
161: array(2) {
162: [0]=>
163: string(5) "water"
164: [1]=>
165: string(5) "fruit"
166: }
167: int(12)
168: bool(false)
169:
170: -- Testing fgetcsv() with file opened using a+t mode --
171: array(2) {
172: [0]=>
173: string(5) "water"
174: [1]=>
175: string(5) "fruit"
176: }
177: int(12)
178: bool(false)
179:
180: -- Testing fgetcsv() with file opened using w+ mode --
181: array(2) {
182: [0]=>
183: string(5) "water"
184: [1]=>
185: string(5) "fruit"
186: }
187: int(12)
188: bool(false)
189:
190: -- Testing fgetcsv() with file opened using w+b mode --
191: array(2) {
192: [0]=>
193: string(5) "water"
194: [1]=>
195: string(5) "fruit"
196: }
197: int(12)
198: bool(false)
199:
200: -- Testing fgetcsv() with file opened using w+t mode --
201: array(2) {
202: [0]=>
203: string(5) "water"
204: [1]=>
205: string(5) "fruit"
206: }
207: int(12)
208: bool(false)
209:
210: -- Testing fgetcsv() with file opened using x+ mode --
211: array(2) {
212: [0]=>
213: string(5) "water"
214: [1]=>
215: string(5) "fruit"
216: }
217: int(12)
218: bool(false)
219:
220: -- Testing fgetcsv() with file opened using x+b mode --
221: array(2) {
222: [0]=>
223: string(5) "water"
224: [1]=>
225: string(5) "fruit"
226: }
227: int(12)
228: bool(false)
229:
230: -- Testing fgetcsv() with file opened using x+t mode --
231: array(2) {
232: [0]=>
233: string(5) "water"
234: [1]=>
235: string(5) "fruit"
236: }
237: int(12)
238: bool(false)
239:
240: -- Testing fgetcsv() with file opened using r mode --
241: array(2) {
242: [0]=>
243: string(5) "water"
244: [1]=>
245: string(5) "fruit"
246: }
247: int(12)
248: bool(false)
249:
250: -- Testing fgetcsv() with file opened using rb mode --
251: array(2) {
252: [0]=>
253: string(5) "water"
254: [1]=>
255: string(5) "fruit"
256: }
257: int(12)
258: bool(false)
259:
260: -- Testing fgetcsv() with file opened using rt mode --
261: array(2) {
262: [0]=>
263: string(5) "water"
264: [1]=>
265: string(5) "fruit"
266: }
267: int(12)
268: bool(false)
269:
270: -- Testing fgetcsv() with file opened using r+ mode --
271: array(2) {
272: [0]=>
273: string(5) "water"
274: [1]=>
275: string(5) "fruit"
276: }
277: int(12)
278: bool(false)
279:
280: -- Testing fgetcsv() with file opened using r+b mode --
281: array(2) {
282: [0]=>
283: string(5) "water"
284: [1]=>
285: string(5) "fruit"
286: }
287: int(12)
288: bool(false)
289:
290: -- Testing fgetcsv() with file opened using r+t mode --
291: array(2) {
292: [0]=>
293: string(5) "water"
294: [1]=>
295: string(5) "fruit"
296: }
297: int(12)
298: bool(false)
299:
300: -- Testing fgetcsv() with file opened using a+ mode --
301: array(2) {
302: [0]=>
303: string(5) "water"
304: [1]=>
305: string(5) "fruit"
306: }
307: int(12)
308: bool(false)
309:
310: -- Testing fgetcsv() with file opened using a+b mode --
311: array(2) {
312: [0]=>
313: string(5) "water"
314: [1]=>
315: string(5) "fruit"
316: }
317: int(12)
318: bool(false)
319:
320: -- Testing fgetcsv() with file opened using a+t mode --
321: array(2) {
322: [0]=>
323: string(5) "water"
324: [1]=>
325: string(5) "fruit"
326: }
327: int(12)
328: bool(false)
329:
330: -- Testing fgetcsv() with file opened using w+ mode --
331: array(2) {
332: [0]=>
333: string(5) "water"
334: [1]=>
335: string(5) "fruit"
336: }
337: int(12)
338: bool(false)
339:
340: -- Testing fgetcsv() with file opened using w+b mode --
341: array(2) {
342: [0]=>
343: string(5) "water"
344: [1]=>
345: string(5) "fruit"
346: }
347: int(12)
348: bool(false)
349:
350: -- Testing fgetcsv() with file opened using w+t mode --
351: array(2) {
352: [0]=>
353: string(5) "water"
354: [1]=>
355: string(5) "fruit"
356: }
357: int(12)
358: bool(false)
359:
360: -- Testing fgetcsv() with file opened using x+ mode --
361: array(2) {
362: [0]=>
363: string(5) "water"
364: [1]=>
365: string(5) "fruit"
366: }
367: int(12)
368: bool(false)
369:
370: -- Testing fgetcsv() with file opened using x+b mode --
371: array(2) {
372: [0]=>
373: string(5) "water"
374: [1]=>
375: string(5) "fruit"
376: }
377: int(12)
378: bool(false)
379:
380: -- Testing fgetcsv() with file opened using x+t mode --
381: array(2) {
382: [0]=>
383: string(5) "water"
384: [1]=>
385: string(5) "fruit"
386: }
387: int(12)
388: bool(false)
389:
390: -- Testing fgetcsv() with file opened using r mode --
391: array(2) {
392: [0]=>
393: string(5) "water"
394: [1]=>
395: string(5) "fruit"
396: }
397: int(16)
398: bool(false)
399:
400: -- Testing fgetcsv() with file opened using rb mode --
401: array(2) {
402: [0]=>
403: string(5) "water"
404: [1]=>
405: string(5) "fruit"
406: }
407: int(16)
408: bool(false)
409:
410: -- Testing fgetcsv() with file opened using rt mode --
411: array(2) {
412: [0]=>
413: string(5) "water"
414: [1]=>
415: string(5) "fruit"
416: }
417: int(16)
418: bool(false)
419:
420: -- Testing fgetcsv() with file opened using r+ mode --
421: array(2) {
422: [0]=>
423: string(5) "water"
424: [1]=>
425: string(5) "fruit"
426: }
427: int(16)
428: bool(false)
429:
430: -- Testing fgetcsv() with file opened using r+b mode --
431: array(2) {
432: [0]=>
433: string(5) "water"
434: [1]=>
435: string(5) "fruit"
436: }
437: int(16)
438: bool(false)
439:
440: -- Testing fgetcsv() with file opened using r+t mode --
441: array(2) {
442: [0]=>
443: string(5) "water"
444: [1]=>
445: string(5) "fruit"
446: }
447: int(16)
448: bool(false)
449:
450: -- Testing fgetcsv() with file opened using a+ mode --
451: array(2) {
452: [0]=>
453: string(5) "water"
454: [1]=>
455: string(5) "fruit"
456: }
457: int(16)
458: bool(false)
459:
460: -- Testing fgetcsv() with file opened using a+b mode --
461: array(2) {
462: [0]=>
463: string(5) "water"
464: [1]=>
465: string(5) "fruit"
466: }
467: int(16)
468: bool(false)
469:
470: -- Testing fgetcsv() with file opened using a+t mode --
471: array(2) {
472: [0]=>
473: string(5) "water"
474: [1]=>
475: string(5) "fruit"
476: }
477: int(16)
478: bool(false)
479:
480: -- Testing fgetcsv() with file opened using w+ mode --
481: array(2) {
482: [0]=>
483: string(5) "water"
484: [1]=>
485: string(5) "fruit"
486: }
487: int(16)
488: bool(false)
489:
490: -- Testing fgetcsv() with file opened using w+b mode --
491: array(2) {
492: [0]=>
493: string(5) "water"
494: [1]=>
495: string(5) "fruit"
496: }
497: int(16)
498: bool(false)
499:
500: -- Testing fgetcsv() with file opened using w+t mode --
501: array(2) {
502: [0]=>
503: string(5) "water"
504: [1]=>
505: string(5) "fruit"
506: }
507: int(16)
508: bool(false)
509:
510: -- Testing fgetcsv() with file opened using x+ mode --
511: array(2) {
512: [0]=>
513: string(5) "water"
514: [1]=>
515: string(5) "fruit"
516: }
517: int(16)
518: bool(false)
519:
520: -- Testing fgetcsv() with file opened using x+b mode --
521: array(2) {
522: [0]=>
523: string(5) "water"
524: [1]=>
525: string(5) "fruit"
526: }
527: int(16)
528: bool(false)
529:
530: -- Testing fgetcsv() with file opened using x+t mode --
531: array(2) {
532: [0]=>
533: string(5) "water"
534: [1]=>
535: string(5) "fruit"
536: }
537: int(16)
538: bool(false)
539:
540: -- Testing fgetcsv() with file opened using r mode --
541: array(3) {
542: [0]=>
543: string(5) "water"
544: [1]=>
545: string(5) "fruit"
546: [2]=>
547: string(3) "air"
548: }
549: int(20)
550: bool(false)
551:
552: -- Testing fgetcsv() with file opened using rb mode --
553: array(3) {
554: [0]=>
555: string(5) "water"
556: [1]=>
557: string(5) "fruit"
558: [2]=>
559: string(3) "air"
560: }
561: int(20)
562: bool(false)
563:
564: -- Testing fgetcsv() with file opened using rt mode --
565: array(3) {
566: [0]=>
567: string(5) "water"
568: [1]=>
569: string(5) "fruit"
570: [2]=>
571: string(3) "air"
572: }
573: int(20)
574: bool(false)
575:
576: -- Testing fgetcsv() with file opened using r+ mode --
577: array(3) {
578: [0]=>
579: string(5) "water"
580: [1]=>
581: string(5) "fruit"
582: [2]=>
583: string(3) "air"
584: }
585: int(20)
586: bool(false)
587:
588: -- Testing fgetcsv() with file opened using r+b mode --
589: array(3) {
590: [0]=>
591: string(5) "water"
592: [1]=>
593: string(5) "fruit"
594: [2]=>
595: string(3) "air"
596: }
597: int(20)
598: bool(false)
599:
600: -- Testing fgetcsv() with file opened using r+t mode --
601: array(3) {
602: [0]=>
603: string(5) "water"
604: [1]=>
605: string(5) "fruit"
606: [2]=>
607: string(3) "air"
608: }
609: int(20)
610: bool(false)
611:
612: -- Testing fgetcsv() with file opened using a+ mode --
613: array(3) {
614: [0]=>
615: string(5) "water"
616: [1]=>
617: string(5) "fruit"
618: [2]=>
619: string(3) "air"
620: }
621: int(20)
622: bool(false)
623:
624: -- Testing fgetcsv() with file opened using a+b mode --
625: array(3) {
626: [0]=>
627: string(5) "water"
628: [1]=>
629: string(5) "fruit"
630: [2]=>
631: string(3) "air"
632: }
633: int(20)
634: bool(false)
635:
636: -- Testing fgetcsv() with file opened using a+t mode --
637: array(3) {
638: [0]=>
639: string(5) "water"
640: [1]=>
641: string(5) "fruit"
642: [2]=>
643: string(3) "air"
644: }
645: int(20)
646: bool(false)
647:
648: -- Testing fgetcsv() with file opened using w+ mode --
649: array(3) {
650: [0]=>
651: string(5) "water"
652: [1]=>
653: string(5) "fruit"
654: [2]=>
655: string(3) "air"
656: }
657: int(20)
658: bool(false)
659:
660: -- Testing fgetcsv() with file opened using w+b mode --
661: array(3) {
662: [0]=>
663: string(5) "water"
664: [1]=>
665: string(5) "fruit"
666: [2]=>
667: string(3) "air"
668: }
669: int(20)
670: bool(false)
671:
672: -- Testing fgetcsv() with file opened using w+t mode --
673: array(3) {
674: [0]=>
675: string(5) "water"
676: [1]=>
677: string(5) "fruit"
678: [2]=>
679: string(3) "air"
680: }
681: int(20)
682: bool(false)
683:
684: -- Testing fgetcsv() with file opened using x+ mode --
685: array(3) {
686: [0]=>
687: string(5) "water"
688: [1]=>
689: string(5) "fruit"
690: [2]=>
691: string(3) "air"
692: }
693: int(20)
694: bool(false)
695:
696: -- Testing fgetcsv() with file opened using x+b mode --
697: array(3) {
698: [0]=>
699: string(5) "water"
700: [1]=>
701: string(5) "fruit"
702: [2]=>
703: string(3) "air"
704: }
705: int(20)
706: bool(false)
707:
708: -- Testing fgetcsv() with file opened using x+t mode --
709: array(3) {
710: [0]=>
711: string(5) "water"
712: [1]=>
713: string(5) "fruit"
714: [2]=>
715: string(3) "air"
716: }
717: int(20)
718: bool(false)
719:
720: -- Testing fgetcsv() with file opened using r mode --
721: array(3) {
722: [0]=>
723: string(5) "water"
724: [1]=>
725: string(5) "fruit"
726: [2]=>
727: string(43) ""
728: This is line of text without csv fields
729:
730: "
731: }
732: int(61)
733: bool(true)
734:
735: -- Testing fgetcsv() with file opened using rb mode --
736: array(3) {
737: [0]=>
738: string(5) "water"
739: [1]=>
740: string(5) "fruit"
741: [2]=>
742: string(43) ""
743: This is line of text without csv fields
744:
745: "
746: }
747: int(61)
748: bool(true)
749:
750: -- Testing fgetcsv() with file opened using rt mode --
751: array(3) {
752: [0]=>
753: string(5) "water"
754: [1]=>
755: string(5) "fruit"
756: [2]=>
757: string(43) ""
758: This is line of text without csv fields
759:
760: "
761: }
762: int(61)
763: bool(true)
764:
765: -- Testing fgetcsv() with file opened using r+ mode --
766: array(3) {
767: [0]=>
768: string(5) "water"
769: [1]=>
770: string(5) "fruit"
771: [2]=>
772: string(43) ""
773: This is line of text without csv fields
774:
775: "
776: }
777: int(61)
778: bool(true)
779:
780: -- Testing fgetcsv() with file opened using r+b mode --
781: array(3) {
782: [0]=>
783: string(5) "water"
784: [1]=>
785: string(5) "fruit"
786: [2]=>
787: string(43) ""
788: This is line of text without csv fields
789:
790: "
791: }
792: int(61)
793: bool(true)
794:
795: -- Testing fgetcsv() with file opened using r+t mode --
796: array(3) {
797: [0]=>
798: string(5) "water"
799: [1]=>
800: string(5) "fruit"
801: [2]=>
802: string(43) ""
803: This is line of text without csv fields
804:
805: "
806: }
807: int(61)
808: bool(true)
809:
810: -- Testing fgetcsv() with file opened using a+ mode --
811: array(3) {
812: [0]=>
813: string(5) "water"
814: [1]=>
815: string(5) "fruit"
816: [2]=>
817: string(43) ""
818: This is line of text without csv fields
819:
820: "
821: }
822: int(61)
823: bool(true)
824:
825: -- Testing fgetcsv() with file opened using a+b mode --
826: array(3) {
827: [0]=>
828: string(5) "water"
829: [1]=>
830: string(5) "fruit"
831: [2]=>
832: string(43) ""
833: This is line of text without csv fields
834:
835: "
836: }
837: int(61)
838: bool(true)
839:
840: -- Testing fgetcsv() with file opened using a+t mode --
841: array(3) {
842: [0]=>
843: string(5) "water"
844: [1]=>
845: string(5) "fruit"
846: [2]=>
847: string(43) ""
848: This is line of text without csv fields
849:
850: "
851: }
852: int(61)
853: bool(true)
854:
855: -- Testing fgetcsv() with file opened using w+ mode --
856: array(3) {
857: [0]=>
858: string(5) "water"
859: [1]=>
860: string(5) "fruit"
861: [2]=>
862: string(43) ""
863: This is line of text without csv fields
864:
865: "
866: }
867: int(61)
868: bool(true)
869:
870: -- Testing fgetcsv() with file opened using w+b mode --
871: array(3) {
872: [0]=>
873: string(5) "water"
874: [1]=>
875: string(5) "fruit"
876: [2]=>
877: string(43) ""
878: This is line of text without csv fields
879:
880: "
881: }
882: int(61)
883: bool(true)
884:
885: -- Testing fgetcsv() with file opened using w+t mode --
886: array(3) {
887: [0]=>
888: string(5) "water"
889: [1]=>
890: string(5) "fruit"
891: [2]=>
892: string(43) ""
893: This is line of text without csv fields
894:
895: "
896: }
897: int(61)
898: bool(true)
899:
900: -- Testing fgetcsv() with file opened using x+ mode --
901: array(3) {
902: [0]=>
903: string(5) "water"
904: [1]=>
905: string(5) "fruit"
906: [2]=>
907: string(43) ""
908: This is line of text without csv fields
909:
910: "
911: }
912: int(61)
913: bool(true)
914:
915: -- Testing fgetcsv() with file opened using x+b mode --
916: array(3) {
917: [0]=>
918: string(5) "water"
919: [1]=>
920: string(5) "fruit"
921: [2]=>
922: string(43) ""
923: This is line of text without csv fields
924:
925: "
926: }
927: int(61)
928: bool(true)
929:
930: -- Testing fgetcsv() with file opened using x+t mode --
931: array(3) {
932: [0]=>
933: string(5) "water"
934: [1]=>
935: string(5) "fruit"
936: [2]=>
937: string(43) ""
938: This is line of text without csv fields
939:
940: "
941: }
942: int(61)
943: bool(true)
944: Done