1--TEST-- 2Test fputcsv() : usage variations - with different delimiter and 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 are different from those 12 present in the field to be written to the file 13 */ 14 15echo "*** Testing fputcsv() : with different delimiter and 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_variation6.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, '+', '%') ); 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 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(15) 167int(15) 168bool(false) 169string(15) ""water","fruit 170" 171 172-- file opened in r+b -- 173int(15) 174int(15) 175bool(false) 176string(15) ""water","fruit 177" 178 179-- file opened in r+t -- 180int(15) 181int(15) 182bool(false) 183string(%d) ""water","fruit 184" 185 186-- file opened in a+ -- 187int(15) 188int(15) 189bool(false) 190string(15) ""water","fruit 191" 192 193-- file opened in a+b -- 194int(15) 195int(15) 196bool(false) 197string(15) ""water","fruit 198" 199 200-- file opened in a+t -- 201int(15) 202int(15) 203bool(false) 204string(%d) ""water","fruit 205" 206 207-- file opened in w+ -- 208int(15) 209int(15) 210bool(false) 211string(15) ""water","fruit 212" 213 214-- file opened in w+b -- 215int(15) 216int(15) 217bool(false) 218string(15) ""water","fruit 219" 220 221-- file opened in w+t -- 222int(15) 223int(15) 224bool(false) 225string(%d) ""water","fruit 226" 227 228-- file opened in x+ -- 229int(15) 230int(15) 231bool(false) 232string(15) ""water","fruit 233" 234 235-- file opened in x+b -- 236int(15) 237int(15) 238bool(false) 239string(15) ""water","fruit 240" 241 242-- file opened in x+t -- 243int(15) 244int(15) 245bool(false) 246string(%d) ""water","fruit 247" 248 249-- file opened in r+ -- 250int(16) 251int(16) 252bool(false) 253string(16) ""water","fruit" 254" 255 256-- file opened in r+b -- 257int(16) 258int(16) 259bool(false) 260string(16) ""water","fruit" 261" 262 263-- file opened in r+t -- 264int(16) 265int(16) 266bool(false) 267string(%d) ""water","fruit" 268" 269 270-- file opened in a+ -- 271int(16) 272int(16) 273bool(false) 274string(16) ""water","fruit" 275" 276 277-- file opened in a+b -- 278int(16) 279int(16) 280bool(false) 281string(16) ""water","fruit" 282" 283 284-- file opened in a+t -- 285int(16) 286int(16) 287bool(false) 288string(%d) ""water","fruit" 289" 290 291-- file opened in w+ -- 292int(16) 293int(16) 294bool(false) 295string(16) ""water","fruit" 296" 297 298-- file opened in w+b -- 299int(16) 300int(16) 301bool(false) 302string(16) ""water","fruit" 303" 304 305-- file opened in w+t -- 306int(16) 307int(16) 308bool(false) 309string(%d) ""water","fruit" 310" 311 312-- file opened in x+ -- 313int(16) 314int(16) 315bool(false) 316string(16) ""water","fruit" 317" 318 319-- file opened in x+b -- 320int(16) 321int(16) 322bool(false) 323string(16) ""water","fruit" 324" 325 326-- file opened in x+t -- 327int(16) 328int(16) 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(16) 419int(16) 420bool(false) 421string(16) "&water&:&fruit& 422" 423 424-- file opened in r+b -- 425int(16) 426int(16) 427bool(false) 428string(16) "&water&:&fruit& 429" 430 431-- file opened in r+t -- 432int(16) 433int(16) 434bool(false) 435string(%d) "&water&:&fruit& 436" 437 438-- file opened in a+ -- 439int(16) 440int(16) 441bool(false) 442string(16) "&water&:&fruit& 443" 444 445-- file opened in a+b -- 446int(16) 447int(16) 448bool(false) 449string(16) "&water&:&fruit& 450" 451 452-- file opened in a+t -- 453int(16) 454int(16) 455bool(false) 456string(%d) "&water&:&fruit& 457" 458 459-- file opened in w+ -- 460int(16) 461int(16) 462bool(false) 463string(16) "&water&:&fruit& 464" 465 466-- file opened in w+b -- 467int(16) 468int(16) 469bool(false) 470string(16) "&water&:&fruit& 471" 472 473-- file opened in w+t -- 474int(16) 475int(16) 476bool(false) 477string(%d) "&water&:&fruit& 478" 479 480-- file opened in x+ -- 481int(16) 482int(16) 483bool(false) 484string(16) "&water&:&fruit& 485" 486 487-- file opened in x+b -- 488int(16) 489int(16) 490bool(false) 491string(16) "&water&:&fruit& 492" 493 494-- file opened in x+t -- 495int(16) 496int(16) 497bool(false) 498string(%d) "&water&:&fruit& 499" 500 501-- file opened in r+ -- 502int(16) 503int(16) 504bool(false) 505string(16) "=water===fruit= 506" 507 508-- file opened in r+b -- 509int(16) 510int(16) 511bool(false) 512string(16) "=water===fruit= 513" 514 515-- file opened in r+t -- 516int(16) 517int(16) 518bool(false) 519string(%d) "=water===fruit= 520" 521 522-- file opened in a+ -- 523int(16) 524int(16) 525bool(false) 526string(16) "=water===fruit= 527" 528 529-- file opened in a+b -- 530int(16) 531int(16) 532bool(false) 533string(16) "=water===fruit= 534" 535 536-- file opened in a+t -- 537int(16) 538int(16) 539bool(false) 540string(%d) "=water===fruit= 541" 542 543-- file opened in w+ -- 544int(16) 545int(16) 546bool(false) 547string(16) "=water===fruit= 548" 549 550-- file opened in w+b -- 551int(16) 552int(16) 553bool(false) 554string(16) "=water===fruit= 555" 556 557-- file opened in w+t -- 558int(16) 559int(16) 560bool(false) 561string(%d) "=water===fruit= 562" 563 564-- file opened in x+ -- 565int(16) 566int(16) 567bool(false) 568string(16) "=water===fruit= 569" 570 571-- file opened in x+b -- 572int(16) 573int(16) 574bool(false) 575string(16) "=water===fruit= 576" 577 578-- file opened in x+t -- 579int(16) 580int(16) 581bool(false) 582string(%d) "=water===fruit= 583" 584 585-- file opened in r+ -- 586int(18) 587int(18) 588bool(false) 589string(18) "-water--fruit-air 590" 591 592-- file opened in r+b -- 593int(18) 594int(18) 595bool(false) 596string(18) "-water--fruit-air 597" 598 599-- file opened in r+t -- 600int(18) 601int(18) 602bool(false) 603string(%d) "-water--fruit-air 604" 605 606-- file opened in a+ -- 607int(18) 608int(18) 609bool(false) 610string(18) "-water--fruit-air 611" 612 613-- file opened in a+b -- 614int(18) 615int(18) 616bool(false) 617string(18) "-water--fruit-air 618" 619 620-- file opened in a+t -- 621int(18) 622int(18) 623bool(false) 624string(%d) "-water--fruit-air 625" 626 627-- file opened in w+ -- 628int(18) 629int(18) 630bool(false) 631string(18) "-water--fruit-air 632" 633 634-- file opened in w+b -- 635int(18) 636int(18) 637bool(false) 638string(18) "-water--fruit-air 639" 640 641-- file opened in w+t -- 642int(18) 643int(18) 644bool(false) 645string(%d) "-water--fruit-air 646" 647 648-- file opened in x+ -- 649int(18) 650int(18) 651bool(false) 652string(18) "-water--fruit-air 653" 654 655-- file opened in x+b -- 656int(18) 657int(18) 658bool(false) 659string(18) "-water--fruit-air 660" 661 662-- file opened in x+t -- 663int(18) 664int(18) 665bool(false) 666string(%d) "-water--fruit-air 667" 668 669-- file opened in r+ -- 670int(22) 671int(22) 672bool(false) 673string(22) "-water---fruit---air- 674" 675 676-- file opened in r+b -- 677int(22) 678int(22) 679bool(false) 680string(22) "-water---fruit---air- 681" 682 683-- file opened in r+t -- 684int(22) 685int(22) 686bool(false) 687string(%d) "-water---fruit---air- 688" 689 690-- file opened in a+ -- 691int(22) 692int(22) 693bool(false) 694string(22) "-water---fruit---air- 695" 696 697-- file opened in a+b -- 698int(22) 699int(22) 700bool(false) 701string(22) "-water---fruit---air- 702" 703 704-- file opened in a+t -- 705int(22) 706int(22) 707bool(false) 708string(%d) "-water---fruit---air- 709" 710 711-- file opened in w+ -- 712int(22) 713int(22) 714bool(false) 715string(22) "-water---fruit---air- 716" 717 718-- file opened in w+b -- 719int(22) 720int(22) 721bool(false) 722string(22) "-water---fruit---air- 723" 724 725-- file opened in w+t -- 726int(22) 727int(22) 728bool(false) 729string(%d) "-water---fruit---air- 730" 731 732-- file opened in x+ -- 733int(22) 734int(22) 735bool(false) 736string(22) "-water---fruit---air- 737" 738 739-- file opened in x+b -- 740int(22) 741int(22) 742bool(false) 743string(22) "-water---fruit---air- 744" 745 746-- file opened in x+t -- 747int(22) 748int(22) 749bool(false) 750string(%d) "-water---fruit---air- 751" 752 753-- file opened in r+ -- 754int(24) 755int(24) 756bool(false) 757string(24) "&""""&:&"&:,:":&,&:,,,, 758" 759 760-- file opened in r+b -- 761int(24) 762int(24) 763bool(false) 764string(24) "&""""&:&"&:,:":&,&:,,,, 765" 766 767-- file opened in r+t -- 768int(24) 769int(24) 770bool(false) 771string(%d) "&""""&:&"&:,:":&,&:,,,, 772" 773 774-- file opened in a+ -- 775int(24) 776int(24) 777bool(false) 778string(24) "&""""&:&"&:,:":&,&:,,,, 779" 780 781-- file opened in a+b -- 782int(24) 783int(24) 784bool(false) 785string(24) "&""""&:&"&:,:":&,&:,,,, 786" 787 788-- file opened in a+t -- 789int(24) 790int(24) 791bool(false) 792string(%d) "&""""&:&"&:,:":&,&:,,,, 793" 794 795-- file opened in w+ -- 796int(24) 797int(24) 798bool(false) 799string(24) "&""""&:&"&:,:":&,&:,,,, 800" 801 802-- file opened in w+b -- 803int(24) 804int(24) 805bool(false) 806string(24) "&""""&:&"&:,:":&,&:,,,, 807" 808 809-- file opened in w+t -- 810int(24) 811int(24) 812bool(false) 813string(%d) "&""""&:&"&:,:":&,&:,,,, 814" 815 816-- file opened in x+ -- 817int(24) 818int(24) 819bool(false) 820string(24) "&""""&:&"&:,:":&,&:,,,, 821" 822 823-- file opened in x+b -- 824int(24) 825int(24) 826bool(false) 827string(24) "&""""&:&"&:,:":&,&:,,,, 828" 829 830-- file opened in x+t -- 831int(24) 832int(24) 833bool(false) 834string(%d) "&""""&:&"&:,:":&,&:,,,, 835" 836Done 837 838