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