1--TEST-- 2Test fputcsv() : usage variations - with same delimiter and different enclosure 3--FILE-- 4<?php 5 6/* 7 Testing fputcsv() to write to a file when delimiter is same but enclosure is different from those 8 present in the field to be written to the file 9 */ 10 11echo "*** Testing fputcsv() : with same delimiter and different enclosure ***\n"; 12 13/* the array is with three elements in it. Each element should be read as 14 1st element is delimiter, 2nd element is enclosure 15 and 3rd element is csv fields 16*/ 17$csv_lists = array ( 18 array(',', '"', array('water,fruit') ), 19 array(',', '"', array('"water","fruit') ), 20 array(',', '"', array('"water","fruit"') ), 21 array(' ', '^', array('^water^ ^fruit^')), 22 array(':', '&', array('&water&:&fruit&')), 23 array('=', '=', array('=water===fruit=')), 24 array('-', '-', array('-water--fruit-air')), 25 array('-', '-', array('-water---fruit---air-')), 26 array(':', '&', array('&""""&:&"&:,:":&,&:,,,,')) 27 28); 29$file_path = __DIR__; 30$file = "$file_path/fputcsv_variation8.tmp"; 31 32$file_modes = array ("r+", "r+b", "r+t", 33 "a+", "a+b", "a+t", 34 "w+", "w+b", "w+t", 35 "x+", "x+b", "x+t"); 36 37$loop_counter = 1; 38foreach ($csv_lists as $csv_list) { 39 for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) { 40 41 echo "\n-- file opened in $file_modes[$mode_counter] --\n"; 42 // create the file and add the content with has csv fields 43 if ( strstr($file_modes[$mode_counter], "r") ) { 44 $fo = new SplFileObject($file, 'w'); 45 } else { 46 $fo = new SplFileObject($file, $file_modes[$mode_counter]); 47 } 48 $delimiter = $csv_list[0]; 49 $enclosure = $csv_list[1]; 50 $csv_field = $csv_list[2]; 51 52 // write to a file in csv format 53 var_dump( $fo->fputcsv($csv_field, $delimiter, '+') ); 54 // check the file pointer position and eof 55 var_dump( $fo->ftell() ); 56 var_dump( $fo->eof() ); 57 //close the file 58 unset($fo); 59 60 // print the file contents 61 var_dump( file_get_contents($file) ); 62 63 //delete file 64 unlink($file); 65 } //end of mode loop 66} // end of foreach 67 68echo "Done\n"; 69?> 70--EXPECTF-- 71*** Testing fputcsv() : with same delimiter and different enclosure *** 72 73-- file opened in r+ -- 74int(14) 75int(14) 76bool(false) 77string(14) "+water,fruit+ 78" 79 80-- file opened in r+b -- 81int(14) 82int(14) 83bool(false) 84string(14) "+water,fruit+ 85" 86 87-- file opened in r+t -- 88int(14) 89int(14) 90bool(false) 91string(%d) "+water,fruit+ 92" 93 94-- file opened in a+ -- 95int(14) 96int(14) 97bool(false) 98string(14) "+water,fruit+ 99" 100 101-- file opened in a+b -- 102int(14) 103int(14) 104bool(false) 105string(14) "+water,fruit+ 106" 107 108-- file opened in a+t -- 109int(14) 110int(14) 111bool(false) 112string(%d) "+water,fruit+ 113" 114 115-- file opened in w+ -- 116int(14) 117int(14) 118bool(false) 119string(14) "+water,fruit+ 120" 121 122-- file opened in w+b -- 123int(14) 124int(14) 125bool(false) 126string(14) "+water,fruit+ 127" 128 129-- file opened in w+t -- 130int(14) 131int(14) 132bool(false) 133string(%d) "+water,fruit+ 134" 135 136-- file opened in x+ -- 137int(14) 138int(14) 139bool(false) 140string(14) "+water,fruit+ 141" 142 143-- file opened in x+b -- 144int(14) 145int(14) 146bool(false) 147string(14) "+water,fruit+ 148" 149 150-- file opened in x+t -- 151int(14) 152int(14) 153bool(false) 154string(%d) "+water,fruit+ 155" 156 157-- file opened in r+ -- 158int(17) 159int(17) 160bool(false) 161string(17) "+"water","fruit+ 162" 163 164-- file opened in r+b -- 165int(17) 166int(17) 167bool(false) 168string(17) "+"water","fruit+ 169" 170 171-- file opened in r+t -- 172int(17) 173int(17) 174bool(false) 175string(%d) "+"water","fruit+ 176" 177 178-- file opened in a+ -- 179int(17) 180int(17) 181bool(false) 182string(17) "+"water","fruit+ 183" 184 185-- file opened in a+b -- 186int(17) 187int(17) 188bool(false) 189string(17) "+"water","fruit+ 190" 191 192-- file opened in a+t -- 193int(17) 194int(17) 195bool(false) 196string(%d) "+"water","fruit+ 197" 198 199-- file opened in w+ -- 200int(17) 201int(17) 202bool(false) 203string(17) "+"water","fruit+ 204" 205 206-- file opened in w+b -- 207int(17) 208int(17) 209bool(false) 210string(17) "+"water","fruit+ 211" 212 213-- file opened in w+t -- 214int(17) 215int(17) 216bool(false) 217string(%d) "+"water","fruit+ 218" 219 220-- file opened in x+ -- 221int(17) 222int(17) 223bool(false) 224string(17) "+"water","fruit+ 225" 226 227-- file opened in x+b -- 228int(17) 229int(17) 230bool(false) 231string(17) "+"water","fruit+ 232" 233 234-- file opened in x+t -- 235int(17) 236int(17) 237bool(false) 238string(%d) "+"water","fruit+ 239" 240 241-- file opened in r+ -- 242int(18) 243int(18) 244bool(false) 245string(18) "+"water","fruit"+ 246" 247 248-- file opened in r+b -- 249int(18) 250int(18) 251bool(false) 252string(18) "+"water","fruit"+ 253" 254 255-- file opened in r+t -- 256int(18) 257int(18) 258bool(false) 259string(%d) "+"water","fruit"+ 260" 261 262-- file opened in a+ -- 263int(18) 264int(18) 265bool(false) 266string(18) "+"water","fruit"+ 267" 268 269-- file opened in a+b -- 270int(18) 271int(18) 272bool(false) 273string(18) "+"water","fruit"+ 274" 275 276-- file opened in a+t -- 277int(18) 278int(18) 279bool(false) 280string(%d) "+"water","fruit"+ 281" 282 283-- file opened in w+ -- 284int(18) 285int(18) 286bool(false) 287string(18) "+"water","fruit"+ 288" 289 290-- file opened in w+b -- 291int(18) 292int(18) 293bool(false) 294string(18) "+"water","fruit"+ 295" 296 297-- file opened in w+t -- 298int(18) 299int(18) 300bool(false) 301string(%d) "+"water","fruit"+ 302" 303 304-- file opened in x+ -- 305int(18) 306int(18) 307bool(false) 308string(18) "+"water","fruit"+ 309" 310 311-- file opened in x+b -- 312int(18) 313int(18) 314bool(false) 315string(18) "+"water","fruit"+ 316" 317 318-- file opened in x+t -- 319int(18) 320int(18) 321bool(false) 322string(%d) "+"water","fruit"+ 323" 324 325-- file opened in r+ -- 326int(18) 327int(18) 328bool(false) 329string(18) "+^water^ ^fruit^+ 330" 331 332-- file opened in r+b -- 333int(18) 334int(18) 335bool(false) 336string(18) "+^water^ ^fruit^+ 337" 338 339-- file opened in r+t -- 340int(18) 341int(18) 342bool(false) 343string(%d) "+^water^ ^fruit^+ 344" 345 346-- file opened in a+ -- 347int(18) 348int(18) 349bool(false) 350string(18) "+^water^ ^fruit^+ 351" 352 353-- file opened in a+b -- 354int(18) 355int(18) 356bool(false) 357string(18) "+^water^ ^fruit^+ 358" 359 360-- file opened in a+t -- 361int(18) 362int(18) 363bool(false) 364string(%d) "+^water^ ^fruit^+ 365" 366 367-- file opened in w+ -- 368int(18) 369int(18) 370bool(false) 371string(18) "+^water^ ^fruit^+ 372" 373 374-- file opened in w+b -- 375int(18) 376int(18) 377bool(false) 378string(18) "+^water^ ^fruit^+ 379" 380 381-- file opened in w+t -- 382int(18) 383int(18) 384bool(false) 385string(%d) "+^water^ ^fruit^+ 386" 387 388-- file opened in x+ -- 389int(18) 390int(18) 391bool(false) 392string(18) "+^water^ ^fruit^+ 393" 394 395-- file opened in x+b -- 396int(18) 397int(18) 398bool(false) 399string(18) "+^water^ ^fruit^+ 400" 401 402-- file opened in x+t -- 403int(18) 404int(18) 405bool(false) 406string(%d) "+^water^ ^fruit^+ 407" 408 409-- file opened in r+ -- 410int(18) 411int(18) 412bool(false) 413string(18) "+&water&:&fruit&+ 414" 415 416-- file opened in r+b -- 417int(18) 418int(18) 419bool(false) 420string(18) "+&water&:&fruit&+ 421" 422 423-- file opened in r+t -- 424int(18) 425int(18) 426bool(false) 427string(%d) "+&water&:&fruit&+ 428" 429 430-- file opened in a+ -- 431int(18) 432int(18) 433bool(false) 434string(18) "+&water&:&fruit&+ 435" 436 437-- file opened in a+b -- 438int(18) 439int(18) 440bool(false) 441string(18) "+&water&:&fruit&+ 442" 443 444-- file opened in a+t -- 445int(18) 446int(18) 447bool(false) 448string(%d) "+&water&:&fruit&+ 449" 450 451-- file opened in w+ -- 452int(18) 453int(18) 454bool(false) 455string(18) "+&water&:&fruit&+ 456" 457 458-- file opened in w+b -- 459int(18) 460int(18) 461bool(false) 462string(18) "+&water&:&fruit&+ 463" 464 465-- file opened in w+t -- 466int(18) 467int(18) 468bool(false) 469string(%d) "+&water&:&fruit&+ 470" 471 472-- file opened in x+ -- 473int(18) 474int(18) 475bool(false) 476string(18) "+&water&:&fruit&+ 477" 478 479-- file opened in x+b -- 480int(18) 481int(18) 482bool(false) 483string(18) "+&water&:&fruit&+ 484" 485 486-- file opened in x+t -- 487int(18) 488int(18) 489bool(false) 490string(%d) "+&water&:&fruit&+ 491" 492 493-- file opened in r+ -- 494int(18) 495int(18) 496bool(false) 497string(18) "+=water===fruit=+ 498" 499 500-- file opened in r+b -- 501int(18) 502int(18) 503bool(false) 504string(18) "+=water===fruit=+ 505" 506 507-- file opened in r+t -- 508int(18) 509int(18) 510bool(false) 511string(%d) "+=water===fruit=+ 512" 513 514-- file opened in a+ -- 515int(18) 516int(18) 517bool(false) 518string(18) "+=water===fruit=+ 519" 520 521-- file opened in a+b -- 522int(18) 523int(18) 524bool(false) 525string(18) "+=water===fruit=+ 526" 527 528-- file opened in a+t -- 529int(18) 530int(18) 531bool(false) 532string(%d) "+=water===fruit=+ 533" 534 535-- file opened in w+ -- 536int(18) 537int(18) 538bool(false) 539string(18) "+=water===fruit=+ 540" 541 542-- file opened in w+b -- 543int(18) 544int(18) 545bool(false) 546string(18) "+=water===fruit=+ 547" 548 549-- file opened in w+t -- 550int(18) 551int(18) 552bool(false) 553string(%d) "+=water===fruit=+ 554" 555 556-- file opened in x+ -- 557int(18) 558int(18) 559bool(false) 560string(18) "+=water===fruit=+ 561" 562 563-- file opened in x+b -- 564int(18) 565int(18) 566bool(false) 567string(18) "+=water===fruit=+ 568" 569 570-- file opened in x+t -- 571int(18) 572int(18) 573bool(false) 574string(%d) "+=water===fruit=+ 575" 576 577-- file opened in r+ -- 578int(20) 579int(20) 580bool(false) 581string(20) "+-water--fruit-air+ 582" 583 584-- file opened in r+b -- 585int(20) 586int(20) 587bool(false) 588string(20) "+-water--fruit-air+ 589" 590 591-- file opened in r+t -- 592int(20) 593int(20) 594bool(false) 595string(%d) "+-water--fruit-air+ 596" 597 598-- file opened in a+ -- 599int(20) 600int(20) 601bool(false) 602string(20) "+-water--fruit-air+ 603" 604 605-- file opened in a+b -- 606int(20) 607int(20) 608bool(false) 609string(20) "+-water--fruit-air+ 610" 611 612-- file opened in a+t -- 613int(20) 614int(20) 615bool(false) 616string(%d) "+-water--fruit-air+ 617" 618 619-- file opened in w+ -- 620int(20) 621int(20) 622bool(false) 623string(20) "+-water--fruit-air+ 624" 625 626-- file opened in w+b -- 627int(20) 628int(20) 629bool(false) 630string(20) "+-water--fruit-air+ 631" 632 633-- file opened in w+t -- 634int(20) 635int(20) 636bool(false) 637string(%d) "+-water--fruit-air+ 638" 639 640-- file opened in x+ -- 641int(20) 642int(20) 643bool(false) 644string(20) "+-water--fruit-air+ 645" 646 647-- file opened in x+b -- 648int(20) 649int(20) 650bool(false) 651string(20) "+-water--fruit-air+ 652" 653 654-- file opened in x+t -- 655int(20) 656int(20) 657bool(false) 658string(%d) "+-water--fruit-air+ 659" 660 661-- file opened in r+ -- 662int(24) 663int(24) 664bool(false) 665string(24) "+-water---fruit---air-+ 666" 667 668-- file opened in r+b -- 669int(24) 670int(24) 671bool(false) 672string(24) "+-water---fruit---air-+ 673" 674 675-- file opened in r+t -- 676int(24) 677int(24) 678bool(false) 679string(%d) "+-water---fruit---air-+ 680" 681 682-- file opened in a+ -- 683int(24) 684int(24) 685bool(false) 686string(24) "+-water---fruit---air-+ 687" 688 689-- file opened in a+b -- 690int(24) 691int(24) 692bool(false) 693string(24) "+-water---fruit---air-+ 694" 695 696-- file opened in a+t -- 697int(24) 698int(24) 699bool(false) 700string(%d) "+-water---fruit---air-+ 701" 702 703-- file opened in w+ -- 704int(24) 705int(24) 706bool(false) 707string(24) "+-water---fruit---air-+ 708" 709 710-- file opened in w+b -- 711int(24) 712int(24) 713bool(false) 714string(24) "+-water---fruit---air-+ 715" 716 717-- file opened in w+t -- 718int(24) 719int(24) 720bool(false) 721string(%d) "+-water---fruit---air-+ 722" 723 724-- file opened in x+ -- 725int(24) 726int(24) 727bool(false) 728string(24) "+-water---fruit---air-+ 729" 730 731-- file opened in x+b -- 732int(24) 733int(24) 734bool(false) 735string(24) "+-water---fruit---air-+ 736" 737 738-- file opened in x+t -- 739int(24) 740int(24) 741bool(false) 742string(%d) "+-water---fruit---air-+ 743" 744 745-- file opened in r+ -- 746int(26) 747int(26) 748bool(false) 749string(26) "+&""""&:&"&:,:":&,&:,,,,+ 750" 751 752-- file opened in r+b -- 753int(26) 754int(26) 755bool(false) 756string(26) "+&""""&:&"&:,:":&,&:,,,,+ 757" 758 759-- file opened in r+t -- 760int(26) 761int(26) 762bool(false) 763string(%d) "+&""""&:&"&:,:":&,&:,,,,+ 764" 765 766-- file opened in a+ -- 767int(26) 768int(26) 769bool(false) 770string(26) "+&""""&:&"&:,:":&,&:,,,,+ 771" 772 773-- file opened in a+b -- 774int(26) 775int(26) 776bool(false) 777string(26) "+&""""&:&"&:,:":&,&:,,,,+ 778" 779 780-- file opened in a+t -- 781int(26) 782int(26) 783bool(false) 784string(%d) "+&""""&:&"&:,:":&,&:,,,,+ 785" 786 787-- file opened in w+ -- 788int(26) 789int(26) 790bool(false) 791string(26) "+&""""&:&"&:,:":&,&:,,,,+ 792" 793 794-- file opened in w+b -- 795int(26) 796int(26) 797bool(false) 798string(26) "+&""""&:&"&:,:":&,&:,,,,+ 799" 800 801-- file opened in w+t -- 802int(26) 803int(26) 804bool(false) 805string(%d) "+&""""&:&"&:,:":&,&:,,,,+ 806" 807 808-- file opened in x+ -- 809int(26) 810int(26) 811bool(false) 812string(26) "+&""""&:&"&:,:":&,&:,,,,+ 813" 814 815-- file opened in x+b -- 816int(26) 817int(26) 818bool(false) 819string(26) "+&""""&:&"&:,:":&,&:,,,,+ 820" 821 822-- file opened in x+t -- 823int(26) 824int(26) 825bool(false) 826string(%d) "+&""""&:&"&:,:":&,&:,,,,+ 827" 828Done 829