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