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