1--TEST-- 2Test fscanf() function: usage variations - char formats with arrays 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 arrays using different char format types */ 12 13$file_path = dirname(__FILE__); 14 15echo "*** Test fscanf(): different char format types with arrays ***\n"; 16 17// create a file 18$filename = "$file_path/fscanf_variation23.tmp"; 19$file_handle = fopen($filename, "w"); 20if($file_handle == false) 21 exit("Error:failed to open file $filename"); 22 23// array types 24$array_types = array ( 25 array(), 26 array(0), 27 array(1), 28 array(NULL), 29 array(null), 30 array("string"), 31 array(true), 32 array(TRUE), 33 array(false), 34 array(FALSE), 35 array(1,2,3,4), 36 array(1 => "One", "two" => 2) 37); 38 39$char_formats = array( "%c", 40 "%hc", "%lc", "%Lc", 41 " %c", "%c ", "% c", 42 "\t%c", "\n%c", "%4c", 43 "%30c", "%[a-zA-Z@#$&0-9]", "%*c"); 44 45$counter = 1; 46 47// writing to the file 48foreach($array_types as $value) { 49 @fprintf($file_handle, $value); 50 @fprintf($file_handle, "\n"); 51} 52// closing the file 53fclose($file_handle); 54 55// opening the file for reading 56$file_handle = fopen($filename, "r"); 57if($file_handle == false) { 58 exit("Error:failed to open file $filename"); 59} 60 61$counter = 1; 62// reading the values from file using different char formats 63foreach($char_formats as $char_format) { 64 // rewind the file so that for every foreach iteration the file pointer starts from bof 65 rewind($file_handle); 66 echo "\n-- iteration $counter --\n"; 67 while( !feof($file_handle) ) { 68 var_dump( fscanf($file_handle,$char_format) ); 69 } 70 $counter++; 71} 72 73echo "\n*** Done ***"; 74?> 75--CLEAN-- 76<?php 77$file_path = dirname(__FILE__); 78$filename = "$file_path/fscanf_variation23.tmp"; 79unlink($filename); 80?> 81--EXPECTF-- 82*** Test fscanf(): different char format types with arrays *** 83 84-- iteration 1 -- 85array(1) { 86 [0]=> 87 string(1) "A" 88} 89array(1) { 90 [0]=> 91 string(1) "A" 92} 93array(1) { 94 [0]=> 95 string(1) "A" 96} 97array(1) { 98 [0]=> 99 string(1) "A" 100} 101array(1) { 102 [0]=> 103 string(1) "A" 104} 105array(1) { 106 [0]=> 107 string(1) "A" 108} 109array(1) { 110 [0]=> 111 string(1) "A" 112} 113array(1) { 114 [0]=> 115 string(1) "A" 116} 117array(1) { 118 [0]=> 119 string(1) "A" 120} 121array(1) { 122 [0]=> 123 string(1) "A" 124} 125array(1) { 126 [0]=> 127 string(1) "A" 128} 129array(1) { 130 [0]=> 131 string(1) "A" 132} 133bool(false) 134 135-- iteration 2 -- 136array(1) { 137 [0]=> 138 string(1) "A" 139} 140array(1) { 141 [0]=> 142 string(1) "A" 143} 144array(1) { 145 [0]=> 146 string(1) "A" 147} 148array(1) { 149 [0]=> 150 string(1) "A" 151} 152array(1) { 153 [0]=> 154 string(1) "A" 155} 156array(1) { 157 [0]=> 158 string(1) "A" 159} 160array(1) { 161 [0]=> 162 string(1) "A" 163} 164array(1) { 165 [0]=> 166 string(1) "A" 167} 168array(1) { 169 [0]=> 170 string(1) "A" 171} 172array(1) { 173 [0]=> 174 string(1) "A" 175} 176array(1) { 177 [0]=> 178 string(1) "A" 179} 180array(1) { 181 [0]=> 182 string(1) "A" 183} 184bool(false) 185 186-- iteration 3 -- 187array(1) { 188 [0]=> 189 string(1) "A" 190} 191array(1) { 192 [0]=> 193 string(1) "A" 194} 195array(1) { 196 [0]=> 197 string(1) "A" 198} 199array(1) { 200 [0]=> 201 string(1) "A" 202} 203array(1) { 204 [0]=> 205 string(1) "A" 206} 207array(1) { 208 [0]=> 209 string(1) "A" 210} 211array(1) { 212 [0]=> 213 string(1) "A" 214} 215array(1) { 216 [0]=> 217 string(1) "A" 218} 219array(1) { 220 [0]=> 221 string(1) "A" 222} 223array(1) { 224 [0]=> 225 string(1) "A" 226} 227array(1) { 228 [0]=> 229 string(1) "A" 230} 231array(1) { 232 [0]=> 233 string(1) "A" 234} 235bool(false) 236 237-- iteration 4 -- 238array(1) { 239 [0]=> 240 string(1) "A" 241} 242array(1) { 243 [0]=> 244 string(1) "A" 245} 246array(1) { 247 [0]=> 248 string(1) "A" 249} 250array(1) { 251 [0]=> 252 string(1) "A" 253} 254array(1) { 255 [0]=> 256 string(1) "A" 257} 258array(1) { 259 [0]=> 260 string(1) "A" 261} 262array(1) { 263 [0]=> 264 string(1) "A" 265} 266array(1) { 267 [0]=> 268 string(1) "A" 269} 270array(1) { 271 [0]=> 272 string(1) "A" 273} 274array(1) { 275 [0]=> 276 string(1) "A" 277} 278array(1) { 279 [0]=> 280 string(1) "A" 281} 282array(1) { 283 [0]=> 284 string(1) "A" 285} 286bool(false) 287 288-- iteration 5 -- 289array(1) { 290 [0]=> 291 string(1) "A" 292} 293array(1) { 294 [0]=> 295 string(1) "A" 296} 297array(1) { 298 [0]=> 299 string(1) "A" 300} 301array(1) { 302 [0]=> 303 string(1) "A" 304} 305array(1) { 306 [0]=> 307 string(1) "A" 308} 309array(1) { 310 [0]=> 311 string(1) "A" 312} 313array(1) { 314 [0]=> 315 string(1) "A" 316} 317array(1) { 318 [0]=> 319 string(1) "A" 320} 321array(1) { 322 [0]=> 323 string(1) "A" 324} 325array(1) { 326 [0]=> 327 string(1) "A" 328} 329array(1) { 330 [0]=> 331 string(1) "A" 332} 333array(1) { 334 [0]=> 335 string(1) "A" 336} 337bool(false) 338 339-- iteration 6 -- 340array(1) { 341 [0]=> 342 string(1) "A" 343} 344array(1) { 345 [0]=> 346 string(1) "A" 347} 348array(1) { 349 [0]=> 350 string(1) "A" 351} 352array(1) { 353 [0]=> 354 string(1) "A" 355} 356array(1) { 357 [0]=> 358 string(1) "A" 359} 360array(1) { 361 [0]=> 362 string(1) "A" 363} 364array(1) { 365 [0]=> 366 string(1) "A" 367} 368array(1) { 369 [0]=> 370 string(1) "A" 371} 372array(1) { 373 [0]=> 374 string(1) "A" 375} 376array(1) { 377 [0]=> 378 string(1) "A" 379} 380array(1) { 381 [0]=> 382 string(1) "A" 383} 384array(1) { 385 [0]=> 386 string(1) "A" 387} 388bool(false) 389 390-- iteration 7 -- 391 392Warning: fscanf(): Bad scan conversion character " " in %s on line %d 393NULL 394 395Warning: fscanf(): Bad scan conversion character " " in %s on line %d 396NULL 397 398Warning: fscanf(): Bad scan conversion character " " in %s on line %d 399NULL 400 401Warning: fscanf(): Bad scan conversion character " " in %s on line %d 402NULL 403 404Warning: fscanf(): Bad scan conversion character " " in %s on line %d 405NULL 406 407Warning: fscanf(): Bad scan conversion character " " in %s on line %d 408NULL 409 410Warning: fscanf(): Bad scan conversion character " " in %s on line %d 411NULL 412 413Warning: fscanf(): Bad scan conversion character " " in %s on line %d 414NULL 415 416Warning: fscanf(): Bad scan conversion character " " in %s on line %d 417NULL 418 419Warning: fscanf(): Bad scan conversion character " " in %s on line %d 420NULL 421 422Warning: fscanf(): Bad scan conversion character " " in %s on line %d 423NULL 424 425Warning: fscanf(): Bad scan conversion character " " in %s on line %d 426NULL 427bool(false) 428 429-- iteration 8 -- 430array(1) { 431 [0]=> 432 string(1) "A" 433} 434array(1) { 435 [0]=> 436 string(1) "A" 437} 438array(1) { 439 [0]=> 440 string(1) "A" 441} 442array(1) { 443 [0]=> 444 string(1) "A" 445} 446array(1) { 447 [0]=> 448 string(1) "A" 449} 450array(1) { 451 [0]=> 452 string(1) "A" 453} 454array(1) { 455 [0]=> 456 string(1) "A" 457} 458array(1) { 459 [0]=> 460 string(1) "A" 461} 462array(1) { 463 [0]=> 464 string(1) "A" 465} 466array(1) { 467 [0]=> 468 string(1) "A" 469} 470array(1) { 471 [0]=> 472 string(1) "A" 473} 474array(1) { 475 [0]=> 476 string(1) "A" 477} 478bool(false) 479 480-- iteration 9 -- 481array(1) { 482 [0]=> 483 string(1) "A" 484} 485array(1) { 486 [0]=> 487 string(1) "A" 488} 489array(1) { 490 [0]=> 491 string(1) "A" 492} 493array(1) { 494 [0]=> 495 string(1) "A" 496} 497array(1) { 498 [0]=> 499 string(1) "A" 500} 501array(1) { 502 [0]=> 503 string(1) "A" 504} 505array(1) { 506 [0]=> 507 string(1) "A" 508} 509array(1) { 510 [0]=> 511 string(1) "A" 512} 513array(1) { 514 [0]=> 515 string(1) "A" 516} 517array(1) { 518 [0]=> 519 string(1) "A" 520} 521array(1) { 522 [0]=> 523 string(1) "A" 524} 525array(1) { 526 [0]=> 527 string(1) "A" 528} 529bool(false) 530 531-- iteration 10 -- 532array(1) { 533 [0]=> 534 string(4) "Arra" 535} 536array(1) { 537 [0]=> 538 string(4) "Arra" 539} 540array(1) { 541 [0]=> 542 string(4) "Arra" 543} 544array(1) { 545 [0]=> 546 string(4) "Arra" 547} 548array(1) { 549 [0]=> 550 string(4) "Arra" 551} 552array(1) { 553 [0]=> 554 string(4) "Arra" 555} 556array(1) { 557 [0]=> 558 string(4) "Arra" 559} 560array(1) { 561 [0]=> 562 string(4) "Arra" 563} 564array(1) { 565 [0]=> 566 string(4) "Arra" 567} 568array(1) { 569 [0]=> 570 string(4) "Arra" 571} 572array(1) { 573 [0]=> 574 string(4) "Arra" 575} 576array(1) { 577 [0]=> 578 string(4) "Arra" 579} 580bool(false) 581 582-- iteration 11 -- 583array(1) { 584 [0]=> 585 string(5) "Array" 586} 587array(1) { 588 [0]=> 589 string(5) "Array" 590} 591array(1) { 592 [0]=> 593 string(5) "Array" 594} 595array(1) { 596 [0]=> 597 string(5) "Array" 598} 599array(1) { 600 [0]=> 601 string(5) "Array" 602} 603array(1) { 604 [0]=> 605 string(5) "Array" 606} 607array(1) { 608 [0]=> 609 string(5) "Array" 610} 611array(1) { 612 [0]=> 613 string(5) "Array" 614} 615array(1) { 616 [0]=> 617 string(5) "Array" 618} 619array(1) { 620 [0]=> 621 string(5) "Array" 622} 623array(1) { 624 [0]=> 625 string(5) "Array" 626} 627array(1) { 628 [0]=> 629 string(5) "Array" 630} 631bool(false) 632 633-- iteration 12 -- 634array(1) { 635 [0]=> 636 string(5) "Array" 637} 638array(1) { 639 [0]=> 640 string(5) "Array" 641} 642array(1) { 643 [0]=> 644 string(5) "Array" 645} 646array(1) { 647 [0]=> 648 string(5) "Array" 649} 650array(1) { 651 [0]=> 652 string(5) "Array" 653} 654array(1) { 655 [0]=> 656 string(5) "Array" 657} 658array(1) { 659 [0]=> 660 string(5) "Array" 661} 662array(1) { 663 [0]=> 664 string(5) "Array" 665} 666array(1) { 667 [0]=> 668 string(5) "Array" 669} 670array(1) { 671 [0]=> 672 string(5) "Array" 673} 674array(1) { 675 [0]=> 676 string(5) "Array" 677} 678array(1) { 679 [0]=> 680 string(5) "Array" 681} 682bool(false) 683 684-- iteration 13 -- 685array(0) { 686} 687array(0) { 688} 689array(0) { 690} 691array(0) { 692} 693array(0) { 694} 695array(0) { 696} 697array(0) { 698} 699array(0) { 700} 701array(0) { 702} 703array(0) { 704} 705array(0) { 706} 707array(0) { 708} 709bool(false) 710 711*** Done *** 712