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