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