1--TEST-- 2Test fgetcsv() : usage variations - with enclosure of two chars 3--FILE-- 4<?php 5/* 6 Testing fgetcsv() to read a file when provided with default enclosure character 7 and with delimiter of two characters 8*/ 9 10echo "*** Testing fgetcsv() : with default enclosure & delimiter of two chars ***\n"; 11 12/* the array is with two elements in it. Each element should be read as 13 1st element is delimiter & 2nd element is csv fields 14*/ 15$csv_lists = array ( 16 array(',', 'water,fruit'), 17 array(' ', 'water fruit'), 18 array(' ', '"water" "fruit"'), 19 array('\\', 'water\\"fruit"\\"air"'), 20 array('\\', '"water"\\"fruit"\\"""'), 21); 22 23$filename = __DIR__ . '/fgetcsv_variation19.tmp'; 24@unlink($filename); 25 26$file_modes = array ("r","rb", "rt", "r+", "r+b", "r+t", 27 "a+", "a+b", "a+t", 28 "w+", "w+b", "w+t", 29 "x+", "x+b", "x+t"); 30 31$loop_counter = 1; 32foreach ($csv_lists as $csv_list) { 33 for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) { 34 // create the file and add the content with has csv fields 35 if ( strstr($file_modes[$mode_counter], "r") ) { 36 $file_handle = fopen($filename, "w"); 37 } else { 38 $file_handle = fopen($filename, $file_modes[$mode_counter] ); 39 } 40 if ( !$file_handle ) { 41 echo "Error: failed to create file $filename!\n"; 42 exit(); 43 } 44 $delimiter = $csv_list[0]; 45 $csv_field = $csv_list[1]; 46 fwrite($file_handle, $csv_field . "\n"); 47 // write another line of text and a blank line 48 // this will be used to test, if the fgetcsv() read more than a line and its 49 // working when only a blank line is read 50 fwrite($file_handle, "This is line of text without csv fields\n"); 51 fwrite($file_handle, "\n"); // blank line 52 53 // close the file if the mode to be used is read mode and re-open using read mode 54 // else rewind the file pointer to beginning of the file 55 if ( strstr($file_modes[$mode_counter], "r" ) ) { 56 fclose($file_handle); 57 $file_handle = fopen($filename, $file_modes[$mode_counter]); 58 } else { 59 // rewind the file pointer to bof 60 rewind($file_handle); 61 } 62 63 echo "\n-- Testing fgetcsv() with file opened using $file_modes[$mode_counter] mode --\n"; 64 65 // call fgetcsv() to parse csv fields 66 67 // use delimiter & enclosure char of two chars 68 fseek($file_handle, 0, SEEK_SET); 69 $del = "++"; 70 try { 71 var_dump( fgetcsv($file_handle, 1024, $del) ); 72 } catch (ValueError $e) { 73 echo $e->getMessage(), "\n"; 74 } 75 // check the file pointer position and if eof 76 var_dump( ftell($file_handle) ); 77 var_dump( feof($file_handle) ); 78 79 // close the file 80 fclose($file_handle); 81 //delete file 82 unlink($filename); 83 } //end of mode loop 84} // end of foreach 85 86echo "Done\n"; 87?> 88--EXPECT-- 89*** Testing fgetcsv() : with default enclosure & delimiter of two chars *** 90 91-- Testing fgetcsv() with file opened using r mode -- 92fgetcsv(): Argument #3 ($separator) must be a single character 93int(0) 94bool(false) 95 96-- Testing fgetcsv() with file opened using rb mode -- 97fgetcsv(): Argument #3 ($separator) must be a single character 98int(0) 99bool(false) 100 101-- Testing fgetcsv() with file opened using rt mode -- 102fgetcsv(): Argument #3 ($separator) must be a single character 103int(0) 104bool(false) 105 106-- Testing fgetcsv() with file opened using r+ mode -- 107fgetcsv(): Argument #3 ($separator) must be a single character 108int(0) 109bool(false) 110 111-- Testing fgetcsv() with file opened using r+b mode -- 112fgetcsv(): Argument #3 ($separator) must be a single character 113int(0) 114bool(false) 115 116-- Testing fgetcsv() with file opened using r+t mode -- 117fgetcsv(): Argument #3 ($separator) must be a single character 118int(0) 119bool(false) 120 121-- Testing fgetcsv() with file opened using a+ mode -- 122fgetcsv(): Argument #3 ($separator) must be a single character 123int(0) 124bool(false) 125 126-- Testing fgetcsv() with file opened using a+b mode -- 127fgetcsv(): Argument #3 ($separator) must be a single character 128int(0) 129bool(false) 130 131-- Testing fgetcsv() with file opened using a+t mode -- 132fgetcsv(): Argument #3 ($separator) must be a single character 133int(0) 134bool(false) 135 136-- Testing fgetcsv() with file opened using w+ mode -- 137fgetcsv(): Argument #3 ($separator) must be a single character 138int(0) 139bool(false) 140 141-- Testing fgetcsv() with file opened using w+b mode -- 142fgetcsv(): Argument #3 ($separator) must be a single character 143int(0) 144bool(false) 145 146-- Testing fgetcsv() with file opened using w+t mode -- 147fgetcsv(): Argument #3 ($separator) must be a single character 148int(0) 149bool(false) 150 151-- Testing fgetcsv() with file opened using x+ mode -- 152fgetcsv(): Argument #3 ($separator) must be a single character 153int(0) 154bool(false) 155 156-- Testing fgetcsv() with file opened using x+b mode -- 157fgetcsv(): Argument #3 ($separator) must be a single character 158int(0) 159bool(false) 160 161-- Testing fgetcsv() with file opened using x+t mode -- 162fgetcsv(): Argument #3 ($separator) must be a single character 163int(0) 164bool(false) 165 166-- Testing fgetcsv() with file opened using r mode -- 167fgetcsv(): Argument #3 ($separator) must be a single character 168int(0) 169bool(false) 170 171-- Testing fgetcsv() with file opened using rb mode -- 172fgetcsv(): Argument #3 ($separator) must be a single character 173int(0) 174bool(false) 175 176-- Testing fgetcsv() with file opened using rt mode -- 177fgetcsv(): Argument #3 ($separator) must be a single character 178int(0) 179bool(false) 180 181-- Testing fgetcsv() with file opened using r+ mode -- 182fgetcsv(): Argument #3 ($separator) must be a single character 183int(0) 184bool(false) 185 186-- Testing fgetcsv() with file opened using r+b mode -- 187fgetcsv(): Argument #3 ($separator) must be a single character 188int(0) 189bool(false) 190 191-- Testing fgetcsv() with file opened using r+t mode -- 192fgetcsv(): Argument #3 ($separator) must be a single character 193int(0) 194bool(false) 195 196-- Testing fgetcsv() with file opened using a+ mode -- 197fgetcsv(): Argument #3 ($separator) must be a single character 198int(0) 199bool(false) 200 201-- Testing fgetcsv() with file opened using a+b mode -- 202fgetcsv(): Argument #3 ($separator) must be a single character 203int(0) 204bool(false) 205 206-- Testing fgetcsv() with file opened using a+t mode -- 207fgetcsv(): Argument #3 ($separator) must be a single character 208int(0) 209bool(false) 210 211-- Testing fgetcsv() with file opened using w+ mode -- 212fgetcsv(): Argument #3 ($separator) must be a single character 213int(0) 214bool(false) 215 216-- Testing fgetcsv() with file opened using w+b mode -- 217fgetcsv(): Argument #3 ($separator) must be a single character 218int(0) 219bool(false) 220 221-- Testing fgetcsv() with file opened using w+t mode -- 222fgetcsv(): Argument #3 ($separator) must be a single character 223int(0) 224bool(false) 225 226-- Testing fgetcsv() with file opened using x+ mode -- 227fgetcsv(): Argument #3 ($separator) must be a single character 228int(0) 229bool(false) 230 231-- Testing fgetcsv() with file opened using x+b mode -- 232fgetcsv(): Argument #3 ($separator) must be a single character 233int(0) 234bool(false) 235 236-- Testing fgetcsv() with file opened using x+t mode -- 237fgetcsv(): Argument #3 ($separator) must be a single character 238int(0) 239bool(false) 240 241-- Testing fgetcsv() with file opened using r mode -- 242fgetcsv(): Argument #3 ($separator) must be a single character 243int(0) 244bool(false) 245 246-- Testing fgetcsv() with file opened using rb mode -- 247fgetcsv(): Argument #3 ($separator) must be a single character 248int(0) 249bool(false) 250 251-- Testing fgetcsv() with file opened using rt mode -- 252fgetcsv(): Argument #3 ($separator) must be a single character 253int(0) 254bool(false) 255 256-- Testing fgetcsv() with file opened using r+ mode -- 257fgetcsv(): Argument #3 ($separator) must be a single character 258int(0) 259bool(false) 260 261-- Testing fgetcsv() with file opened using r+b mode -- 262fgetcsv(): Argument #3 ($separator) must be a single character 263int(0) 264bool(false) 265 266-- Testing fgetcsv() with file opened using r+t mode -- 267fgetcsv(): Argument #3 ($separator) must be a single character 268int(0) 269bool(false) 270 271-- Testing fgetcsv() with file opened using a+ mode -- 272fgetcsv(): Argument #3 ($separator) must be a single character 273int(0) 274bool(false) 275 276-- Testing fgetcsv() with file opened using a+b mode -- 277fgetcsv(): Argument #3 ($separator) must be a single character 278int(0) 279bool(false) 280 281-- Testing fgetcsv() with file opened using a+t mode -- 282fgetcsv(): Argument #3 ($separator) must be a single character 283int(0) 284bool(false) 285 286-- Testing fgetcsv() with file opened using w+ mode -- 287fgetcsv(): Argument #3 ($separator) must be a single character 288int(0) 289bool(false) 290 291-- Testing fgetcsv() with file opened using w+b mode -- 292fgetcsv(): Argument #3 ($separator) must be a single character 293int(0) 294bool(false) 295 296-- Testing fgetcsv() with file opened using w+t mode -- 297fgetcsv(): Argument #3 ($separator) must be a single character 298int(0) 299bool(false) 300 301-- Testing fgetcsv() with file opened using x+ mode -- 302fgetcsv(): Argument #3 ($separator) must be a single character 303int(0) 304bool(false) 305 306-- Testing fgetcsv() with file opened using x+b mode -- 307fgetcsv(): Argument #3 ($separator) must be a single character 308int(0) 309bool(false) 310 311-- Testing fgetcsv() with file opened using x+t mode -- 312fgetcsv(): Argument #3 ($separator) must be a single character 313int(0) 314bool(false) 315 316-- Testing fgetcsv() with file opened using r mode -- 317fgetcsv(): Argument #3 ($separator) must be a single character 318int(0) 319bool(false) 320 321-- Testing fgetcsv() with file opened using rb mode -- 322fgetcsv(): Argument #3 ($separator) must be a single character 323int(0) 324bool(false) 325 326-- Testing fgetcsv() with file opened using rt mode -- 327fgetcsv(): Argument #3 ($separator) must be a single character 328int(0) 329bool(false) 330 331-- Testing fgetcsv() with file opened using r+ mode -- 332fgetcsv(): Argument #3 ($separator) must be a single character 333int(0) 334bool(false) 335 336-- Testing fgetcsv() with file opened using r+b mode -- 337fgetcsv(): Argument #3 ($separator) must be a single character 338int(0) 339bool(false) 340 341-- Testing fgetcsv() with file opened using r+t mode -- 342fgetcsv(): Argument #3 ($separator) must be a single character 343int(0) 344bool(false) 345 346-- Testing fgetcsv() with file opened using a+ mode -- 347fgetcsv(): Argument #3 ($separator) must be a single character 348int(0) 349bool(false) 350 351-- Testing fgetcsv() with file opened using a+b mode -- 352fgetcsv(): Argument #3 ($separator) must be a single character 353int(0) 354bool(false) 355 356-- Testing fgetcsv() with file opened using a+t mode -- 357fgetcsv(): Argument #3 ($separator) must be a single character 358int(0) 359bool(false) 360 361-- Testing fgetcsv() with file opened using w+ mode -- 362fgetcsv(): Argument #3 ($separator) must be a single character 363int(0) 364bool(false) 365 366-- Testing fgetcsv() with file opened using w+b mode -- 367fgetcsv(): Argument #3 ($separator) must be a single character 368int(0) 369bool(false) 370 371-- Testing fgetcsv() with file opened using w+t mode -- 372fgetcsv(): Argument #3 ($separator) must be a single character 373int(0) 374bool(false) 375 376-- Testing fgetcsv() with file opened using x+ mode -- 377fgetcsv(): Argument #3 ($separator) must be a single character 378int(0) 379bool(false) 380 381-- Testing fgetcsv() with file opened using x+b mode -- 382fgetcsv(): Argument #3 ($separator) must be a single character 383int(0) 384bool(false) 385 386-- Testing fgetcsv() with file opened using x+t mode -- 387fgetcsv(): Argument #3 ($separator) must be a single character 388int(0) 389bool(false) 390 391-- Testing fgetcsv() with file opened using r mode -- 392fgetcsv(): Argument #3 ($separator) must be a single character 393int(0) 394bool(false) 395 396-- Testing fgetcsv() with file opened using rb mode -- 397fgetcsv(): Argument #3 ($separator) must be a single character 398int(0) 399bool(false) 400 401-- Testing fgetcsv() with file opened using rt mode -- 402fgetcsv(): Argument #3 ($separator) must be a single character 403int(0) 404bool(false) 405 406-- Testing fgetcsv() with file opened using r+ mode -- 407fgetcsv(): Argument #3 ($separator) must be a single character 408int(0) 409bool(false) 410 411-- Testing fgetcsv() with file opened using r+b mode -- 412fgetcsv(): Argument #3 ($separator) must be a single character 413int(0) 414bool(false) 415 416-- Testing fgetcsv() with file opened using r+t mode -- 417fgetcsv(): Argument #3 ($separator) must be a single character 418int(0) 419bool(false) 420 421-- Testing fgetcsv() with file opened using a+ mode -- 422fgetcsv(): Argument #3 ($separator) must be a single character 423int(0) 424bool(false) 425 426-- Testing fgetcsv() with file opened using a+b mode -- 427fgetcsv(): Argument #3 ($separator) must be a single character 428int(0) 429bool(false) 430 431-- Testing fgetcsv() with file opened using a+t mode -- 432fgetcsv(): Argument #3 ($separator) must be a single character 433int(0) 434bool(false) 435 436-- Testing fgetcsv() with file opened using w+ mode -- 437fgetcsv(): Argument #3 ($separator) must be a single character 438int(0) 439bool(false) 440 441-- Testing fgetcsv() with file opened using w+b mode -- 442fgetcsv(): Argument #3 ($separator) must be a single character 443int(0) 444bool(false) 445 446-- Testing fgetcsv() with file opened using w+t mode -- 447fgetcsv(): Argument #3 ($separator) must be a single character 448int(0) 449bool(false) 450 451-- Testing fgetcsv() with file opened using x+ mode -- 452fgetcsv(): Argument #3 ($separator) must be a single character 453int(0) 454bool(false) 455 456-- Testing fgetcsv() with file opened using x+b mode -- 457fgetcsv(): Argument #3 ($separator) must be a single character 458int(0) 459bool(false) 460 461-- Testing fgetcsv() with file opened using x+t mode -- 462fgetcsv(): Argument #3 ($separator) must be a single character 463int(0) 464bool(false) 465Done 466