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