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