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