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