1--TEST--
2Test fscanf() function: usage variations - string formats with strings
3--FILE--
4<?php
5
6/*
7  Prototype: mixed fscanf ( resource $handle, string $format [, mixed &$...] );
8  Description: Parses input from a file according to a format
9*/
10
11/* Test fscanf() to scan different strings using different string format types */
12
13$file_path = dirname(__FILE__);
14
15echo "*** Test fscanf(): different string format types with different string ***\n";
16
17// create a file
18$filename = "$file_path/fscanf_variation14.tmp";
19$file_handle = fopen($filename, "w");
20if($file_handle == false)
21  exit("Error:failed to open file $filename");
22
23// different valid strings
24
25/* string created using Heredoc (<<<) */
26$heredoc_string = <<<EOT
27This is string defined
28using heredoc.
29EOT;
30/* heredoc string with only numerics */
31$heredoc_numeric_string = <<<EOT
32123456 3993
334849 string
34EOT;
35/* null heardoc string */
36$heredoc_empty_string = <<<EOT
37EOT;
38$heredoc_null_string = <<<EOT
39NULL
40EOT;
41
42$valid_strings = array(
43  "",
44  " ",
45  '',
46  ' ',
47  "string",
48  'string',
49  "NULL",
50  'null',
51  "FALSE",
52  'true',
53  "\x0b",
54  "\0",
55  '\0',
56  '\060',
57  "\070",
58  "0x55F",
59  "055",
60  "@#$#$%%$^^$%^%^$^&",
61  $heredoc_string,
62  $heredoc_numeric_string,
63  $heredoc_empty_string,
64  $heredoc_null_string
65);
66
67// various string formats
68$string_formats = array( "%s",
69                         "%hs", "%ls", "%Ls",
70                         " %s", "%s ", "% s",
71                         "\t%s", "\n%s", "%4s",
72                         "%30s", "%[a-zA-Z0-9]", "%*s"
73                  );
74
75$counter = 1;
76
77// writing to the file
78foreach($valid_strings as $string) {
79  @fprintf($file_handle, $string);
80  @fprintf($file_handle, "\n");
81}
82// closing the file
83fclose($file_handle);
84
85// opening the file for reading
86$file_handle = fopen($filename, "r");
87if($file_handle == false) {
88  exit("Error:failed to open file $filename");
89}
90
91$counter = 1;
92// reading the values from file using different string formats
93foreach($string_formats as $string_format) {
94  // rewind the file so that for every foreach iteration the file pointer starts from bof
95  rewind($file_handle);
96  echo "\n-- iteration $counter --\n";
97  while( !feof($file_handle) ) {
98    var_dump( fscanf($file_handle,$string_format) );
99  }
100  $counter++;
101}
102
103echo "\n*** Done ***";
104?>
105--CLEAN--
106<?php
107$file_path = dirname(__FILE__);
108$filename = "$file_path/fscanf_variation14.tmp";
109unlink($filename);
110?>
111--EXPECTF--
112*** Test fscanf(): different string format types with different string ***
113
114-- iteration 1 --
115NULL
116NULL
117NULL
118NULL
119array(1) {
120  [0]=>
121  string(6) "string"
122}
123array(1) {
124  [0]=>
125  string(6) "string"
126}
127array(1) {
128  [0]=>
129  string(4) "NULL"
130}
131array(1) {
132  [0]=>
133  string(4) "null"
134}
135array(1) {
136  [0]=>
137  string(5) "FALSE"
138}
139array(1) {
140  [0]=>
141  string(4) "true"
142}
143NULL
144NULL
145array(1) {
146  [0]=>
147  string(2) "\0"
148}
149array(1) {
150  [0]=>
151  string(4) "\060"
152}
153array(1) {
154  [0]=>
155  string(1) "8"
156}
157array(1) {
158  [0]=>
159  string(5) "0x55F"
160}
161array(1) {
162  [0]=>
163  string(3) "055"
164}
165NULL
166array(1) {
167  [0]=>
168  string(4) "This"
169}
170array(1) {
171  [0]=>
172  string(5) "using"
173}
174array(1) {
175  [0]=>
176  string(6) "123456"
177}
178array(1) {
179  [0]=>
180  string(4) "4849"
181}
182NULL
183array(1) {
184  [0]=>
185  string(4) "NULL"
186}
187bool(false)
188
189-- iteration 2 --
190NULL
191NULL
192NULL
193NULL
194array(1) {
195  [0]=>
196  string(6) "string"
197}
198array(1) {
199  [0]=>
200  string(6) "string"
201}
202array(1) {
203  [0]=>
204  string(4) "NULL"
205}
206array(1) {
207  [0]=>
208  string(4) "null"
209}
210array(1) {
211  [0]=>
212  string(5) "FALSE"
213}
214array(1) {
215  [0]=>
216  string(4) "true"
217}
218NULL
219NULL
220array(1) {
221  [0]=>
222  string(2) "\0"
223}
224array(1) {
225  [0]=>
226  string(4) "\060"
227}
228array(1) {
229  [0]=>
230  string(1) "8"
231}
232array(1) {
233  [0]=>
234  string(5) "0x55F"
235}
236array(1) {
237  [0]=>
238  string(3) "055"
239}
240NULL
241array(1) {
242  [0]=>
243  string(4) "This"
244}
245array(1) {
246  [0]=>
247  string(5) "using"
248}
249array(1) {
250  [0]=>
251  string(6) "123456"
252}
253array(1) {
254  [0]=>
255  string(4) "4849"
256}
257NULL
258array(1) {
259  [0]=>
260  string(4) "NULL"
261}
262bool(false)
263
264-- iteration 3 --
265NULL
266NULL
267NULL
268NULL
269array(1) {
270  [0]=>
271  string(6) "string"
272}
273array(1) {
274  [0]=>
275  string(6) "string"
276}
277array(1) {
278  [0]=>
279  string(4) "NULL"
280}
281array(1) {
282  [0]=>
283  string(4) "null"
284}
285array(1) {
286  [0]=>
287  string(5) "FALSE"
288}
289array(1) {
290  [0]=>
291  string(4) "true"
292}
293NULL
294NULL
295array(1) {
296  [0]=>
297  string(2) "\0"
298}
299array(1) {
300  [0]=>
301  string(4) "\060"
302}
303array(1) {
304  [0]=>
305  string(1) "8"
306}
307array(1) {
308  [0]=>
309  string(5) "0x55F"
310}
311array(1) {
312  [0]=>
313  string(3) "055"
314}
315NULL
316array(1) {
317  [0]=>
318  string(4) "This"
319}
320array(1) {
321  [0]=>
322  string(5) "using"
323}
324array(1) {
325  [0]=>
326  string(6) "123456"
327}
328array(1) {
329  [0]=>
330  string(4) "4849"
331}
332NULL
333array(1) {
334  [0]=>
335  string(4) "NULL"
336}
337bool(false)
338
339-- iteration 4 --
340NULL
341NULL
342NULL
343NULL
344array(1) {
345  [0]=>
346  string(6) "string"
347}
348array(1) {
349  [0]=>
350  string(6) "string"
351}
352array(1) {
353  [0]=>
354  string(4) "NULL"
355}
356array(1) {
357  [0]=>
358  string(4) "null"
359}
360array(1) {
361  [0]=>
362  string(5) "FALSE"
363}
364array(1) {
365  [0]=>
366  string(4) "true"
367}
368NULL
369NULL
370array(1) {
371  [0]=>
372  string(2) "\0"
373}
374array(1) {
375  [0]=>
376  string(4) "\060"
377}
378array(1) {
379  [0]=>
380  string(1) "8"
381}
382array(1) {
383  [0]=>
384  string(5) "0x55F"
385}
386array(1) {
387  [0]=>
388  string(3) "055"
389}
390NULL
391array(1) {
392  [0]=>
393  string(4) "This"
394}
395array(1) {
396  [0]=>
397  string(5) "using"
398}
399array(1) {
400  [0]=>
401  string(6) "123456"
402}
403array(1) {
404  [0]=>
405  string(4) "4849"
406}
407NULL
408array(1) {
409  [0]=>
410  string(4) "NULL"
411}
412bool(false)
413
414-- iteration 5 --
415NULL
416NULL
417NULL
418NULL
419array(1) {
420  [0]=>
421  string(6) "string"
422}
423array(1) {
424  [0]=>
425  string(6) "string"
426}
427array(1) {
428  [0]=>
429  string(4) "NULL"
430}
431array(1) {
432  [0]=>
433  string(4) "null"
434}
435array(1) {
436  [0]=>
437  string(5) "FALSE"
438}
439array(1) {
440  [0]=>
441  string(4) "true"
442}
443NULL
444NULL
445array(1) {
446  [0]=>
447  string(2) "\0"
448}
449array(1) {
450  [0]=>
451  string(4) "\060"
452}
453array(1) {
454  [0]=>
455  string(1) "8"
456}
457array(1) {
458  [0]=>
459  string(5) "0x55F"
460}
461array(1) {
462  [0]=>
463  string(3) "055"
464}
465NULL
466array(1) {
467  [0]=>
468  string(4) "This"
469}
470array(1) {
471  [0]=>
472  string(5) "using"
473}
474array(1) {
475  [0]=>
476  string(6) "123456"
477}
478array(1) {
479  [0]=>
480  string(4) "4849"
481}
482NULL
483array(1) {
484  [0]=>
485  string(4) "NULL"
486}
487bool(false)
488
489-- iteration 6 --
490NULL
491NULL
492NULL
493NULL
494array(1) {
495  [0]=>
496  string(6) "string"
497}
498array(1) {
499  [0]=>
500  string(6) "string"
501}
502array(1) {
503  [0]=>
504  string(4) "NULL"
505}
506array(1) {
507  [0]=>
508  string(4) "null"
509}
510array(1) {
511  [0]=>
512  string(5) "FALSE"
513}
514array(1) {
515  [0]=>
516  string(4) "true"
517}
518NULL
519NULL
520array(1) {
521  [0]=>
522  string(2) "\0"
523}
524array(1) {
525  [0]=>
526  string(4) "\060"
527}
528array(1) {
529  [0]=>
530  string(1) "8"
531}
532array(1) {
533  [0]=>
534  string(5) "0x55F"
535}
536array(1) {
537  [0]=>
538  string(3) "055"
539}
540NULL
541array(1) {
542  [0]=>
543  string(4) "This"
544}
545array(1) {
546  [0]=>
547  string(5) "using"
548}
549array(1) {
550  [0]=>
551  string(6) "123456"
552}
553array(1) {
554  [0]=>
555  string(4) "4849"
556}
557NULL
558array(1) {
559  [0]=>
560  string(4) "NULL"
561}
562bool(false)
563
564-- iteration 7 --
565
566Warning: fscanf(): Bad scan conversion character " " in %s on line %d
567NULL
568
569Warning: fscanf(): Bad scan conversion character " " in %s on line %d
570NULL
571
572Warning: fscanf(): Bad scan conversion character " " in %s on line %d
573NULL
574
575Warning: fscanf(): Bad scan conversion character " " in %s on line %d
576NULL
577
578Warning: fscanf(): Bad scan conversion character " " in %s on line %d
579NULL
580
581Warning: fscanf(): Bad scan conversion character " " in %s on line %d
582NULL
583
584Warning: fscanf(): Bad scan conversion character " " in %s on line %d
585NULL
586
587Warning: fscanf(): Bad scan conversion character " " in %s on line %d
588NULL
589
590Warning: fscanf(): Bad scan conversion character " " in %s on line %d
591NULL
592
593Warning: fscanf(): Bad scan conversion character " " in %s on line %d
594NULL
595
596Warning: fscanf(): Bad scan conversion character " " in %s on line %d
597NULL
598
599Warning: fscanf(): Bad scan conversion character " " in %s on line %d
600NULL
601
602Warning: fscanf(): Bad scan conversion character " " in %s on line %d
603NULL
604
605Warning: fscanf(): Bad scan conversion character " " in %s on line %d
606NULL
607
608Warning: fscanf(): Bad scan conversion character " " in %s on line %d
609NULL
610
611Warning: fscanf(): Bad scan conversion character " " in %s on line %d
612NULL
613
614Warning: fscanf(): Bad scan conversion character " " in %s on line %d
615NULL
616
617Warning: fscanf(): Bad scan conversion character " " in %s on line %d
618NULL
619
620Warning: fscanf(): Bad scan conversion character " " in %s on line %d
621NULL
622
623Warning: fscanf(): Bad scan conversion character " " in %s on line %d
624NULL
625
626Warning: fscanf(): Bad scan conversion character " " in %s on line %d
627NULL
628
629Warning: fscanf(): Bad scan conversion character " " in %s on line %d
630NULL
631
632Warning: fscanf(): Bad scan conversion character " " in %s on line %d
633NULL
634
635Warning: fscanf(): Bad scan conversion character " " in %s on line %d
636NULL
637bool(false)
638
639-- iteration 8 --
640NULL
641NULL
642NULL
643NULL
644array(1) {
645  [0]=>
646  string(6) "string"
647}
648array(1) {
649  [0]=>
650  string(6) "string"
651}
652array(1) {
653  [0]=>
654  string(4) "NULL"
655}
656array(1) {
657  [0]=>
658  string(4) "null"
659}
660array(1) {
661  [0]=>
662  string(5) "FALSE"
663}
664array(1) {
665  [0]=>
666  string(4) "true"
667}
668NULL
669NULL
670array(1) {
671  [0]=>
672  string(2) "\0"
673}
674array(1) {
675  [0]=>
676  string(4) "\060"
677}
678array(1) {
679  [0]=>
680  string(1) "8"
681}
682array(1) {
683  [0]=>
684  string(5) "0x55F"
685}
686array(1) {
687  [0]=>
688  string(3) "055"
689}
690NULL
691array(1) {
692  [0]=>
693  string(4) "This"
694}
695array(1) {
696  [0]=>
697  string(5) "using"
698}
699array(1) {
700  [0]=>
701  string(6) "123456"
702}
703array(1) {
704  [0]=>
705  string(4) "4849"
706}
707NULL
708array(1) {
709  [0]=>
710  string(4) "NULL"
711}
712bool(false)
713
714-- iteration 9 --
715NULL
716NULL
717NULL
718NULL
719array(1) {
720  [0]=>
721  string(6) "string"
722}
723array(1) {
724  [0]=>
725  string(6) "string"
726}
727array(1) {
728  [0]=>
729  string(4) "NULL"
730}
731array(1) {
732  [0]=>
733  string(4) "null"
734}
735array(1) {
736  [0]=>
737  string(5) "FALSE"
738}
739array(1) {
740  [0]=>
741  string(4) "true"
742}
743NULL
744NULL
745array(1) {
746  [0]=>
747  string(2) "\0"
748}
749array(1) {
750  [0]=>
751  string(4) "\060"
752}
753array(1) {
754  [0]=>
755  string(1) "8"
756}
757array(1) {
758  [0]=>
759  string(5) "0x55F"
760}
761array(1) {
762  [0]=>
763  string(3) "055"
764}
765NULL
766array(1) {
767  [0]=>
768  string(4) "This"
769}
770array(1) {
771  [0]=>
772  string(5) "using"
773}
774array(1) {
775  [0]=>
776  string(6) "123456"
777}
778array(1) {
779  [0]=>
780  string(4) "4849"
781}
782NULL
783array(1) {
784  [0]=>
785  string(4) "NULL"
786}
787bool(false)
788
789-- iteration 10 --
790NULL
791NULL
792NULL
793NULL
794array(1) {
795  [0]=>
796  string(4) "stri"
797}
798array(1) {
799  [0]=>
800  string(4) "stri"
801}
802array(1) {
803  [0]=>
804  string(4) "NULL"
805}
806array(1) {
807  [0]=>
808  string(4) "null"
809}
810array(1) {
811  [0]=>
812  string(4) "FALS"
813}
814array(1) {
815  [0]=>
816  string(4) "true"
817}
818NULL
819NULL
820array(1) {
821  [0]=>
822  string(2) "\0"
823}
824array(1) {
825  [0]=>
826  string(4) "\060"
827}
828array(1) {
829  [0]=>
830  string(1) "8"
831}
832array(1) {
833  [0]=>
834  string(4) "0x55"
835}
836array(1) {
837  [0]=>
838  string(3) "055"
839}
840NULL
841array(1) {
842  [0]=>
843  string(4) "This"
844}
845array(1) {
846  [0]=>
847  string(4) "usin"
848}
849array(1) {
850  [0]=>
851  string(4) "1234"
852}
853array(1) {
854  [0]=>
855  string(4) "4849"
856}
857NULL
858array(1) {
859  [0]=>
860  string(4) "NULL"
861}
862bool(false)
863
864-- iteration 11 --
865NULL
866NULL
867NULL
868NULL
869array(1) {
870  [0]=>
871  string(6) "string"
872}
873array(1) {
874  [0]=>
875  string(6) "string"
876}
877array(1) {
878  [0]=>
879  string(4) "NULL"
880}
881array(1) {
882  [0]=>
883  string(4) "null"
884}
885array(1) {
886  [0]=>
887  string(5) "FALSE"
888}
889array(1) {
890  [0]=>
891  string(4) "true"
892}
893NULL
894NULL
895array(1) {
896  [0]=>
897  string(2) "\0"
898}
899array(1) {
900  [0]=>
901  string(4) "\060"
902}
903array(1) {
904  [0]=>
905  string(1) "8"
906}
907array(1) {
908  [0]=>
909  string(5) "0x55F"
910}
911array(1) {
912  [0]=>
913  string(3) "055"
914}
915NULL
916array(1) {
917  [0]=>
918  string(4) "This"
919}
920array(1) {
921  [0]=>
922  string(5) "using"
923}
924array(1) {
925  [0]=>
926  string(6) "123456"
927}
928array(1) {
929  [0]=>
930  string(4) "4849"
931}
932NULL
933array(1) {
934  [0]=>
935  string(4) "NULL"
936}
937bool(false)
938
939-- iteration 12 --
940array(1) {
941  [0]=>
942  NULL
943}
944array(1) {
945  [0]=>
946  NULL
947}
948array(1) {
949  [0]=>
950  NULL
951}
952array(1) {
953  [0]=>
954  NULL
955}
956array(1) {
957  [0]=>
958  string(6) "string"
959}
960array(1) {
961  [0]=>
962  string(6) "string"
963}
964array(1) {
965  [0]=>
966  string(4) "NULL"
967}
968array(1) {
969  [0]=>
970  string(4) "null"
971}
972array(1) {
973  [0]=>
974  string(5) "FALSE"
975}
976array(1) {
977  [0]=>
978  string(4) "true"
979}
980array(1) {
981  [0]=>
982  NULL
983}
984NULL
985array(1) {
986  [0]=>
987  NULL
988}
989array(1) {
990  [0]=>
991  NULL
992}
993array(1) {
994  [0]=>
995  string(1) "8"
996}
997array(1) {
998  [0]=>
999  string(5) "0x55F"
1000}
1001array(1) {
1002  [0]=>
1003  string(3) "055"
1004}
1005array(1) {
1006  [0]=>
1007  NULL
1008}
1009array(1) {
1010  [0]=>
1011  string(4) "This"
1012}
1013array(1) {
1014  [0]=>
1015  string(5) "using"
1016}
1017array(1) {
1018  [0]=>
1019  string(6) "123456"
1020}
1021array(1) {
1022  [0]=>
1023  string(4) "4849"
1024}
1025array(1) {
1026  [0]=>
1027  NULL
1028}
1029array(1) {
1030  [0]=>
1031  string(4) "NULL"
1032}
1033bool(false)
1034
1035-- iteration 13 --
1036NULL
1037NULL
1038NULL
1039NULL
1040array(0) {
1041}
1042array(0) {
1043}
1044array(0) {
1045}
1046array(0) {
1047}
1048array(0) {
1049}
1050array(0) {
1051}
1052NULL
1053NULL
1054array(0) {
1055}
1056array(0) {
1057}
1058array(0) {
1059}
1060array(0) {
1061}
1062array(0) {
1063}
1064NULL
1065array(0) {
1066}
1067array(0) {
1068}
1069array(0) {
1070}
1071array(0) {
1072}
1073NULL
1074array(0) {
1075}
1076bool(false)
1077
1078*** Done ***
1079
1080