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