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