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