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