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