fgetcsv_variation12.phpt 13 KB

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