1--TEST-- 2Test fputcsv() : usage variations - with different delimiter and enclosure 3--FILE-- 4<?php 5 6/* 7 Testing fputcsv() to write to a file when delimiter are different from those 8 present in the field to be written to the file 9 */ 10 11echo "*** Testing fputcsv() : with different delimiter and 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_variation6.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, '+', '%') ); 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 different delimiter and enclosure *** 72 73-- file opened in r+ -- 74int(12) 75int(12) 76bool(false) 77string(12) "water,fruit 78" 79 80-- file opened in r+b -- 81int(12) 82int(12) 83bool(false) 84string(12) "water,fruit 85" 86 87-- file opened in r+t -- 88int(12) 89int(12) 90bool(false) 91string(%d) "water,fruit 92" 93 94-- file opened in a+ -- 95int(12) 96int(12) 97bool(false) 98string(12) "water,fruit 99" 100 101-- file opened in a+b -- 102int(12) 103int(12) 104bool(false) 105string(12) "water,fruit 106" 107 108-- file opened in a+t -- 109int(12) 110int(12) 111bool(false) 112string(%d) "water,fruit 113" 114 115-- file opened in w+ -- 116int(12) 117int(12) 118bool(false) 119string(12) "water,fruit 120" 121 122-- file opened in w+b -- 123int(12) 124int(12) 125bool(false) 126string(12) "water,fruit 127" 128 129-- file opened in w+t -- 130int(12) 131int(12) 132bool(false) 133string(%d) "water,fruit 134" 135 136-- file opened in x+ -- 137int(12) 138int(12) 139bool(false) 140string(12) "water,fruit 141" 142 143-- file opened in x+b -- 144int(12) 145int(12) 146bool(false) 147string(12) "water,fruit 148" 149 150-- file opened in x+t -- 151int(12) 152int(12) 153bool(false) 154string(%d) "water,fruit 155" 156 157-- file opened in r+ -- 158int(15) 159int(15) 160bool(false) 161string(15) ""water","fruit 162" 163 164-- file opened in r+b -- 165int(15) 166int(15) 167bool(false) 168string(15) ""water","fruit 169" 170 171-- file opened in r+t -- 172int(15) 173int(15) 174bool(false) 175string(%d) ""water","fruit 176" 177 178-- file opened in a+ -- 179int(15) 180int(15) 181bool(false) 182string(15) ""water","fruit 183" 184 185-- file opened in a+b -- 186int(15) 187int(15) 188bool(false) 189string(15) ""water","fruit 190" 191 192-- file opened in a+t -- 193int(15) 194int(15) 195bool(false) 196string(%d) ""water","fruit 197" 198 199-- file opened in w+ -- 200int(15) 201int(15) 202bool(false) 203string(15) ""water","fruit 204" 205 206-- file opened in w+b -- 207int(15) 208int(15) 209bool(false) 210string(15) ""water","fruit 211" 212 213-- file opened in w+t -- 214int(15) 215int(15) 216bool(false) 217string(%d) ""water","fruit 218" 219 220-- file opened in x+ -- 221int(15) 222int(15) 223bool(false) 224string(15) ""water","fruit 225" 226 227-- file opened in x+b -- 228int(15) 229int(15) 230bool(false) 231string(15) ""water","fruit 232" 233 234-- file opened in x+t -- 235int(15) 236int(15) 237bool(false) 238string(%d) ""water","fruit 239" 240 241-- file opened in r+ -- 242int(16) 243int(16) 244bool(false) 245string(16) ""water","fruit" 246" 247 248-- file opened in r+b -- 249int(16) 250int(16) 251bool(false) 252string(16) ""water","fruit" 253" 254 255-- file opened in r+t -- 256int(16) 257int(16) 258bool(false) 259string(%d) ""water","fruit" 260" 261 262-- file opened in a+ -- 263int(16) 264int(16) 265bool(false) 266string(16) ""water","fruit" 267" 268 269-- file opened in a+b -- 270int(16) 271int(16) 272bool(false) 273string(16) ""water","fruit" 274" 275 276-- file opened in a+t -- 277int(16) 278int(16) 279bool(false) 280string(%d) ""water","fruit" 281" 282 283-- file opened in w+ -- 284int(16) 285int(16) 286bool(false) 287string(16) ""water","fruit" 288" 289 290-- file opened in w+b -- 291int(16) 292int(16) 293bool(false) 294string(16) ""water","fruit" 295" 296 297-- file opened in w+t -- 298int(16) 299int(16) 300bool(false) 301string(%d) ""water","fruit" 302" 303 304-- file opened in x+ -- 305int(16) 306int(16) 307bool(false) 308string(16) ""water","fruit" 309" 310 311-- file opened in x+b -- 312int(16) 313int(16) 314bool(false) 315string(16) ""water","fruit" 316" 317 318-- file opened in x+t -- 319int(16) 320int(16) 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(16) 411int(16) 412bool(false) 413string(16) "&water&:&fruit& 414" 415 416-- file opened in r+b -- 417int(16) 418int(16) 419bool(false) 420string(16) "&water&:&fruit& 421" 422 423-- file opened in r+t -- 424int(16) 425int(16) 426bool(false) 427string(%d) "&water&:&fruit& 428" 429 430-- file opened in a+ -- 431int(16) 432int(16) 433bool(false) 434string(16) "&water&:&fruit& 435" 436 437-- file opened in a+b -- 438int(16) 439int(16) 440bool(false) 441string(16) "&water&:&fruit& 442" 443 444-- file opened in a+t -- 445int(16) 446int(16) 447bool(false) 448string(%d) "&water&:&fruit& 449" 450 451-- file opened in w+ -- 452int(16) 453int(16) 454bool(false) 455string(16) "&water&:&fruit& 456" 457 458-- file opened in w+b -- 459int(16) 460int(16) 461bool(false) 462string(16) "&water&:&fruit& 463" 464 465-- file opened in w+t -- 466int(16) 467int(16) 468bool(false) 469string(%d) "&water&:&fruit& 470" 471 472-- file opened in x+ -- 473int(16) 474int(16) 475bool(false) 476string(16) "&water&:&fruit& 477" 478 479-- file opened in x+b -- 480int(16) 481int(16) 482bool(false) 483string(16) "&water&:&fruit& 484" 485 486-- file opened in x+t -- 487int(16) 488int(16) 489bool(false) 490string(%d) "&water&:&fruit& 491" 492 493-- file opened in r+ -- 494int(16) 495int(16) 496bool(false) 497string(16) "=water===fruit= 498" 499 500-- file opened in r+b -- 501int(16) 502int(16) 503bool(false) 504string(16) "=water===fruit= 505" 506 507-- file opened in r+t -- 508int(16) 509int(16) 510bool(false) 511string(%d) "=water===fruit= 512" 513 514-- file opened in a+ -- 515int(16) 516int(16) 517bool(false) 518string(16) "=water===fruit= 519" 520 521-- file opened in a+b -- 522int(16) 523int(16) 524bool(false) 525string(16) "=water===fruit= 526" 527 528-- file opened in a+t -- 529int(16) 530int(16) 531bool(false) 532string(%d) "=water===fruit= 533" 534 535-- file opened in w+ -- 536int(16) 537int(16) 538bool(false) 539string(16) "=water===fruit= 540" 541 542-- file opened in w+b -- 543int(16) 544int(16) 545bool(false) 546string(16) "=water===fruit= 547" 548 549-- file opened in w+t -- 550int(16) 551int(16) 552bool(false) 553string(%d) "=water===fruit= 554" 555 556-- file opened in x+ -- 557int(16) 558int(16) 559bool(false) 560string(16) "=water===fruit= 561" 562 563-- file opened in x+b -- 564int(16) 565int(16) 566bool(false) 567string(16) "=water===fruit= 568" 569 570-- file opened in x+t -- 571int(16) 572int(16) 573bool(false) 574string(%d) "=water===fruit= 575" 576 577-- file opened in r+ -- 578int(18) 579int(18) 580bool(false) 581string(18) "-water--fruit-air 582" 583 584-- file opened in r+b -- 585int(18) 586int(18) 587bool(false) 588string(18) "-water--fruit-air 589" 590 591-- file opened in r+t -- 592int(18) 593int(18) 594bool(false) 595string(%d) "-water--fruit-air 596" 597 598-- file opened in a+ -- 599int(18) 600int(18) 601bool(false) 602string(18) "-water--fruit-air 603" 604 605-- file opened in a+b -- 606int(18) 607int(18) 608bool(false) 609string(18) "-water--fruit-air 610" 611 612-- file opened in a+t -- 613int(18) 614int(18) 615bool(false) 616string(%d) "-water--fruit-air 617" 618 619-- file opened in w+ -- 620int(18) 621int(18) 622bool(false) 623string(18) "-water--fruit-air 624" 625 626-- file opened in w+b -- 627int(18) 628int(18) 629bool(false) 630string(18) "-water--fruit-air 631" 632 633-- file opened in w+t -- 634int(18) 635int(18) 636bool(false) 637string(%d) "-water--fruit-air 638" 639 640-- file opened in x+ -- 641int(18) 642int(18) 643bool(false) 644string(18) "-water--fruit-air 645" 646 647-- file opened in x+b -- 648int(18) 649int(18) 650bool(false) 651string(18) "-water--fruit-air 652" 653 654-- file opened in x+t -- 655int(18) 656int(18) 657bool(false) 658string(%d) "-water--fruit-air 659" 660 661-- file opened in r+ -- 662int(22) 663int(22) 664bool(false) 665string(22) "-water---fruit---air- 666" 667 668-- file opened in r+b -- 669int(22) 670int(22) 671bool(false) 672string(22) "-water---fruit---air- 673" 674 675-- file opened in r+t -- 676int(22) 677int(22) 678bool(false) 679string(%d) "-water---fruit---air- 680" 681 682-- file opened in a+ -- 683int(22) 684int(22) 685bool(false) 686string(22) "-water---fruit---air- 687" 688 689-- file opened in a+b -- 690int(22) 691int(22) 692bool(false) 693string(22) "-water---fruit---air- 694" 695 696-- file opened in a+t -- 697int(22) 698int(22) 699bool(false) 700string(%d) "-water---fruit---air- 701" 702 703-- file opened in w+ -- 704int(22) 705int(22) 706bool(false) 707string(22) "-water---fruit---air- 708" 709 710-- file opened in w+b -- 711int(22) 712int(22) 713bool(false) 714string(22) "-water---fruit---air- 715" 716 717-- file opened in w+t -- 718int(22) 719int(22) 720bool(false) 721string(%d) "-water---fruit---air- 722" 723 724-- file opened in x+ -- 725int(22) 726int(22) 727bool(false) 728string(22) "-water---fruit---air- 729" 730 731-- file opened in x+b -- 732int(22) 733int(22) 734bool(false) 735string(22) "-water---fruit---air- 736" 737 738-- file opened in x+t -- 739int(22) 740int(22) 741bool(false) 742string(%d) "-water---fruit---air- 743" 744 745-- file opened in r+ -- 746int(24) 747int(24) 748bool(false) 749string(24) "&""""&:&"&:,:":&,&:,,,, 750" 751 752-- file opened in r+b -- 753int(24) 754int(24) 755bool(false) 756string(24) "&""""&:&"&:,:":&,&:,,,, 757" 758 759-- file opened in r+t -- 760int(24) 761int(24) 762bool(false) 763string(%d) "&""""&:&"&:,:":&,&:,,,, 764" 765 766-- file opened in a+ -- 767int(24) 768int(24) 769bool(false) 770string(24) "&""""&:&"&:,:":&,&:,,,, 771" 772 773-- file opened in a+b -- 774int(24) 775int(24) 776bool(false) 777string(24) "&""""&:&"&:,:":&,&:,,,, 778" 779 780-- file opened in a+t -- 781int(24) 782int(24) 783bool(false) 784string(%d) "&""""&:&"&:,:":&,&:,,,, 785" 786 787-- file opened in w+ -- 788int(24) 789int(24) 790bool(false) 791string(24) "&""""&:&"&:,:":&,&:,,,, 792" 793 794-- file opened in w+b -- 795int(24) 796int(24) 797bool(false) 798string(24) "&""""&:&"&:,:":&,&:,,,, 799" 800 801-- file opened in w+t -- 802int(24) 803int(24) 804bool(false) 805string(%d) "&""""&:&"&:,:":&,&:,,,, 806" 807 808-- file opened in x+ -- 809int(24) 810int(24) 811bool(false) 812string(24) "&""""&:&"&:,:":&,&:,,,, 813" 814 815-- file opened in x+b -- 816int(24) 817int(24) 818bool(false) 819string(24) "&""""&:&"&:,:":&,&:,,,, 820" 821 822-- file opened in x+t -- 823int(24) 824int(24) 825bool(false) 826string(%d) "&""""&:&"&:,:":&,&:,,,, 827" 828Done 829