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