fgetcsv_variation18.phpt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. --TEST--
  2. Test fgetcsv() : usage variations - with default enclosure and different delimiter
  3. --FILE--
  4. <?php
  5. /*
  6. Prototype: array fgetcsv ( resource $handle [, int $length [, string $delimiter [, string $enclosure]]] );
  7. Description: Gets line from file pointer and parse for CSV fields
  8. */
  9. /* Testing fgetcsv() to read a file when provided with default enclosure character
  10. and with delimiter character which is not in the line being read by fgetcsv()
  11. */
  12. echo "*** Testing fgetcsv() : with default enclosure and different delimiter ***\n";
  13. /* the array is with two elements in it. Each element should be read as
  14. 1st element is delimiter & 2nd element is csv fields
  15. */
  16. $csv_lists = array (
  17. array(',', 'water,fruit'),
  18. array(' ', 'water fruit'),
  19. array(' ', '"water" "fruit"'),
  20. array('\\', 'water\\"fruit"\\"air"'),
  21. array('\\', '"water"\\"fruit"\\"""'),
  22. );
  23. $filename = dirname(__FILE__) . '/fgetcsv_variation18.tmp';
  24. @unlink($filename);
  25. $file_modes = array ("r","rb", "rt", "r+", "r+b", "r+t",
  26. "a+", "a+b", "a+t",
  27. "w+", "w+b", "w+t",
  28. "x+", "x+b", "x+t");
  29. $loop_counter = 1;
  30. foreach ($csv_lists as $csv_list) {
  31. for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) {
  32. // create the file and add the content with has csv fields
  33. if ( strstr($file_modes[$mode_counter], "r") ) {
  34. $file_handle = fopen($filename, "w");
  35. } else {
  36. $file_handle = fopen($filename, $file_modes[$mode_counter] );
  37. }
  38. if ( !$file_handle ) {
  39. echo "Error: failed to create file $filename!\n";
  40. exit();
  41. }
  42. $delimiter = $csv_list[0];
  43. $csv_field = $csv_list[1];
  44. fwrite($file_handle, $csv_field . "\n");
  45. // write another line of text and a blank line
  46. // this will be used to test, if the fgetcsv() read more than a line and its
  47. // working when only a blank line is read
  48. fwrite($file_handle, "This is line of text without csv fields\n");
  49. fwrite($file_handle, "\n"); // blank line
  50. // close the file if the mode to be used is read mode and re-open using read mode
  51. // else rewind the file pointer to beginning of the file
  52. if ( strstr($file_modes[$mode_counter], "r" ) ) {
  53. fclose($file_handle);
  54. $file_handle = fopen($filename, $file_modes[$mode_counter]);
  55. } else {
  56. // rewind the file pointer to bof
  57. rewind($file_handle);
  58. }
  59. echo "\n-- Testing fgetcsv() with file opened using $file_modes[$mode_counter] mode --\n";
  60. // call fgetcsv() to parse csv fields
  61. // use different delimiter than existing in file
  62. fseek($file_handle, 0, SEEK_SET);
  63. $del = "+";
  64. var_dump( fgetcsv($file_handle, 1024, $del) );
  65. // check the file pointer position and if eof
  66. var_dump( ftell($file_handle) );
  67. var_dump( feof($file_handle) );
  68. // close the file
  69. fclose($file_handle);
  70. //delete file
  71. unlink($filename);
  72. } //end of mode loop
  73. } // end of foreach
  74. echo "Done\n";
  75. ?>
  76. --EXPECT--
  77. *** Testing fgetcsv() : with default enclosure and different delimiter ***
  78. -- Testing fgetcsv() with file opened using r mode --
  79. array(1) {
  80. [0]=>
  81. string(11) "water,fruit"
  82. }
  83. int(12)
  84. bool(false)
  85. -- Testing fgetcsv() with file opened using rb mode --
  86. array(1) {
  87. [0]=>
  88. string(11) "water,fruit"
  89. }
  90. int(12)
  91. bool(false)
  92. -- Testing fgetcsv() with file opened using rt mode --
  93. array(1) {
  94. [0]=>
  95. string(11) "water,fruit"
  96. }
  97. int(12)
  98. bool(false)
  99. -- Testing fgetcsv() with file opened using r+ mode --
  100. array(1) {
  101. [0]=>
  102. string(11) "water,fruit"
  103. }
  104. int(12)
  105. bool(false)
  106. -- Testing fgetcsv() with file opened using r+b mode --
  107. array(1) {
  108. [0]=>
  109. string(11) "water,fruit"
  110. }
  111. int(12)
  112. bool(false)
  113. -- Testing fgetcsv() with file opened using r+t mode --
  114. array(1) {
  115. [0]=>
  116. string(11) "water,fruit"
  117. }
  118. int(12)
  119. bool(false)
  120. -- Testing fgetcsv() with file opened using a+ mode --
  121. array(1) {
  122. [0]=>
  123. string(11) "water,fruit"
  124. }
  125. int(12)
  126. bool(false)
  127. -- Testing fgetcsv() with file opened using a+b mode --
  128. array(1) {
  129. [0]=>
  130. string(11) "water,fruit"
  131. }
  132. int(12)
  133. bool(false)
  134. -- Testing fgetcsv() with file opened using a+t mode --
  135. array(1) {
  136. [0]=>
  137. string(11) "water,fruit"
  138. }
  139. int(12)
  140. bool(false)
  141. -- Testing fgetcsv() with file opened using w+ mode --
  142. array(1) {
  143. [0]=>
  144. string(11) "water,fruit"
  145. }
  146. int(12)
  147. bool(false)
  148. -- Testing fgetcsv() with file opened using w+b mode --
  149. array(1) {
  150. [0]=>
  151. string(11) "water,fruit"
  152. }
  153. int(12)
  154. bool(false)
  155. -- Testing fgetcsv() with file opened using w+t mode --
  156. array(1) {
  157. [0]=>
  158. string(11) "water,fruit"
  159. }
  160. int(12)
  161. bool(false)
  162. -- Testing fgetcsv() with file opened using x+ mode --
  163. array(1) {
  164. [0]=>
  165. string(11) "water,fruit"
  166. }
  167. int(12)
  168. bool(false)
  169. -- Testing fgetcsv() with file opened using x+b mode --
  170. array(1) {
  171. [0]=>
  172. string(11) "water,fruit"
  173. }
  174. int(12)
  175. bool(false)
  176. -- Testing fgetcsv() with file opened using x+t mode --
  177. array(1) {
  178. [0]=>
  179. string(11) "water,fruit"
  180. }
  181. int(12)
  182. bool(false)
  183. -- Testing fgetcsv() with file opened using r mode --
  184. array(1) {
  185. [0]=>
  186. string(11) "water fruit"
  187. }
  188. int(12)
  189. bool(false)
  190. -- Testing fgetcsv() with file opened using rb mode --
  191. array(1) {
  192. [0]=>
  193. string(11) "water fruit"
  194. }
  195. int(12)
  196. bool(false)
  197. -- Testing fgetcsv() with file opened using rt mode --
  198. array(1) {
  199. [0]=>
  200. string(11) "water fruit"
  201. }
  202. int(12)
  203. bool(false)
  204. -- Testing fgetcsv() with file opened using r+ mode --
  205. array(1) {
  206. [0]=>
  207. string(11) "water fruit"
  208. }
  209. int(12)
  210. bool(false)
  211. -- Testing fgetcsv() with file opened using r+b mode --
  212. array(1) {
  213. [0]=>
  214. string(11) "water fruit"
  215. }
  216. int(12)
  217. bool(false)
  218. -- Testing fgetcsv() with file opened using r+t mode --
  219. array(1) {
  220. [0]=>
  221. string(11) "water fruit"
  222. }
  223. int(12)
  224. bool(false)
  225. -- Testing fgetcsv() with file opened using a+ mode --
  226. array(1) {
  227. [0]=>
  228. string(11) "water fruit"
  229. }
  230. int(12)
  231. bool(false)
  232. -- Testing fgetcsv() with file opened using a+b mode --
  233. array(1) {
  234. [0]=>
  235. string(11) "water fruit"
  236. }
  237. int(12)
  238. bool(false)
  239. -- Testing fgetcsv() with file opened using a+t mode --
  240. array(1) {
  241. [0]=>
  242. string(11) "water fruit"
  243. }
  244. int(12)
  245. bool(false)
  246. -- Testing fgetcsv() with file opened using w+ mode --
  247. array(1) {
  248. [0]=>
  249. string(11) "water fruit"
  250. }
  251. int(12)
  252. bool(false)
  253. -- Testing fgetcsv() with file opened using w+b mode --
  254. array(1) {
  255. [0]=>
  256. string(11) "water fruit"
  257. }
  258. int(12)
  259. bool(false)
  260. -- Testing fgetcsv() with file opened using w+t mode --
  261. array(1) {
  262. [0]=>
  263. string(11) "water fruit"
  264. }
  265. int(12)
  266. bool(false)
  267. -- Testing fgetcsv() with file opened using x+ mode --
  268. array(1) {
  269. [0]=>
  270. string(11) "water fruit"
  271. }
  272. int(12)
  273. bool(false)
  274. -- Testing fgetcsv() with file opened using x+b mode --
  275. array(1) {
  276. [0]=>
  277. string(11) "water fruit"
  278. }
  279. int(12)
  280. bool(false)
  281. -- Testing fgetcsv() with file opened using x+t mode --
  282. array(1) {
  283. [0]=>
  284. string(11) "water fruit"
  285. }
  286. int(12)
  287. bool(false)
  288. -- Testing fgetcsv() with file opened using r mode --
  289. array(1) {
  290. [0]=>
  291. string(13) "water "fruit""
  292. }
  293. int(16)
  294. bool(false)
  295. -- Testing fgetcsv() with file opened using rb mode --
  296. array(1) {
  297. [0]=>
  298. string(13) "water "fruit""
  299. }
  300. int(16)
  301. bool(false)
  302. -- Testing fgetcsv() with file opened using rt mode --
  303. array(1) {
  304. [0]=>
  305. string(13) "water "fruit""
  306. }
  307. int(16)
  308. bool(false)
  309. -- Testing fgetcsv() with file opened using r+ mode --
  310. array(1) {
  311. [0]=>
  312. string(13) "water "fruit""
  313. }
  314. int(16)
  315. bool(false)
  316. -- Testing fgetcsv() with file opened using r+b mode --
  317. array(1) {
  318. [0]=>
  319. string(13) "water "fruit""
  320. }
  321. int(16)
  322. bool(false)
  323. -- Testing fgetcsv() with file opened using r+t mode --
  324. array(1) {
  325. [0]=>
  326. string(13) "water "fruit""
  327. }
  328. int(16)
  329. bool(false)
  330. -- Testing fgetcsv() with file opened using a+ mode --
  331. array(1) {
  332. [0]=>
  333. string(13) "water "fruit""
  334. }
  335. int(16)
  336. bool(false)
  337. -- Testing fgetcsv() with file opened using a+b mode --
  338. array(1) {
  339. [0]=>
  340. string(13) "water "fruit""
  341. }
  342. int(16)
  343. bool(false)
  344. -- Testing fgetcsv() with file opened using a+t mode --
  345. array(1) {
  346. [0]=>
  347. string(13) "water "fruit""
  348. }
  349. int(16)
  350. bool(false)
  351. -- Testing fgetcsv() with file opened using w+ mode --
  352. array(1) {
  353. [0]=>
  354. string(13) "water "fruit""
  355. }
  356. int(16)
  357. bool(false)
  358. -- Testing fgetcsv() with file opened using w+b mode --
  359. array(1) {
  360. [0]=>
  361. string(13) "water "fruit""
  362. }
  363. int(16)
  364. bool(false)
  365. -- Testing fgetcsv() with file opened using w+t mode --
  366. array(1) {
  367. [0]=>
  368. string(13) "water "fruit""
  369. }
  370. int(16)
  371. bool(false)
  372. -- Testing fgetcsv() with file opened using x+ mode --
  373. array(1) {
  374. [0]=>
  375. string(13) "water "fruit""
  376. }
  377. int(16)
  378. bool(false)
  379. -- Testing fgetcsv() with file opened using x+b mode --
  380. array(1) {
  381. [0]=>
  382. string(13) "water "fruit""
  383. }
  384. int(16)
  385. bool(false)
  386. -- Testing fgetcsv() with file opened using x+t mode --
  387. array(1) {
  388. [0]=>
  389. string(13) "water "fruit""
  390. }
  391. int(16)
  392. bool(false)
  393. -- Testing fgetcsv() with file opened using r mode --
  394. array(1) {
  395. [0]=>
  396. string(19) "water\"fruit"\"air""
  397. }
  398. int(20)
  399. bool(false)
  400. -- Testing fgetcsv() with file opened using rb mode --
  401. array(1) {
  402. [0]=>
  403. string(19) "water\"fruit"\"air""
  404. }
  405. int(20)
  406. bool(false)
  407. -- Testing fgetcsv() with file opened using rt mode --
  408. array(1) {
  409. [0]=>
  410. string(19) "water\"fruit"\"air""
  411. }
  412. int(20)
  413. bool(false)
  414. -- Testing fgetcsv() with file opened using r+ mode --
  415. array(1) {
  416. [0]=>
  417. string(19) "water\"fruit"\"air""
  418. }
  419. int(20)
  420. bool(false)
  421. -- Testing fgetcsv() with file opened using r+b mode --
  422. array(1) {
  423. [0]=>
  424. string(19) "water\"fruit"\"air""
  425. }
  426. int(20)
  427. bool(false)
  428. -- Testing fgetcsv() with file opened using r+t mode --
  429. array(1) {
  430. [0]=>
  431. string(19) "water\"fruit"\"air""
  432. }
  433. int(20)
  434. bool(false)
  435. -- Testing fgetcsv() with file opened using a+ mode --
  436. array(1) {
  437. [0]=>
  438. string(19) "water\"fruit"\"air""
  439. }
  440. int(20)
  441. bool(false)
  442. -- Testing fgetcsv() with file opened using a+b mode --
  443. array(1) {
  444. [0]=>
  445. string(19) "water\"fruit"\"air""
  446. }
  447. int(20)
  448. bool(false)
  449. -- Testing fgetcsv() with file opened using a+t mode --
  450. array(1) {
  451. [0]=>
  452. string(19) "water\"fruit"\"air""
  453. }
  454. int(20)
  455. bool(false)
  456. -- Testing fgetcsv() with file opened using w+ mode --
  457. array(1) {
  458. [0]=>
  459. string(19) "water\"fruit"\"air""
  460. }
  461. int(20)
  462. bool(false)
  463. -- Testing fgetcsv() with file opened using w+b mode --
  464. array(1) {
  465. [0]=>
  466. string(19) "water\"fruit"\"air""
  467. }
  468. int(20)
  469. bool(false)
  470. -- Testing fgetcsv() with file opened using w+t mode --
  471. array(1) {
  472. [0]=>
  473. string(19) "water\"fruit"\"air""
  474. }
  475. int(20)
  476. bool(false)
  477. -- Testing fgetcsv() with file opened using x+ mode --
  478. array(1) {
  479. [0]=>
  480. string(19) "water\"fruit"\"air""
  481. }
  482. int(20)
  483. bool(false)
  484. -- Testing fgetcsv() with file opened using x+b mode --
  485. array(1) {
  486. [0]=>
  487. string(19) "water\"fruit"\"air""
  488. }
  489. int(20)
  490. bool(false)
  491. -- Testing fgetcsv() with file opened using x+t mode --
  492. array(1) {
  493. [0]=>
  494. string(19) "water\"fruit"\"air""
  495. }
  496. int(20)
  497. bool(false)
  498. -- Testing fgetcsv() with file opened using r mode --
  499. array(1) {
  500. [0]=>
  501. string(17) "water\"fruit"\""""
  502. }
  503. int(20)
  504. bool(false)
  505. -- Testing fgetcsv() with file opened using rb mode --
  506. array(1) {
  507. [0]=>
  508. string(17) "water\"fruit"\""""
  509. }
  510. int(20)
  511. bool(false)
  512. -- Testing fgetcsv() with file opened using rt mode --
  513. array(1) {
  514. [0]=>
  515. string(17) "water\"fruit"\""""
  516. }
  517. int(20)
  518. bool(false)
  519. -- Testing fgetcsv() with file opened using r+ mode --
  520. array(1) {
  521. [0]=>
  522. string(17) "water\"fruit"\""""
  523. }
  524. int(20)
  525. bool(false)
  526. -- Testing fgetcsv() with file opened using r+b mode --
  527. array(1) {
  528. [0]=>
  529. string(17) "water\"fruit"\""""
  530. }
  531. int(20)
  532. bool(false)
  533. -- Testing fgetcsv() with file opened using r+t mode --
  534. array(1) {
  535. [0]=>
  536. string(17) "water\"fruit"\""""
  537. }
  538. int(20)
  539. bool(false)
  540. -- Testing fgetcsv() with file opened using a+ mode --
  541. array(1) {
  542. [0]=>
  543. string(17) "water\"fruit"\""""
  544. }
  545. int(20)
  546. bool(false)
  547. -- Testing fgetcsv() with file opened using a+b mode --
  548. array(1) {
  549. [0]=>
  550. string(17) "water\"fruit"\""""
  551. }
  552. int(20)
  553. bool(false)
  554. -- Testing fgetcsv() with file opened using a+t mode --
  555. array(1) {
  556. [0]=>
  557. string(17) "water\"fruit"\""""
  558. }
  559. int(20)
  560. bool(false)
  561. -- Testing fgetcsv() with file opened using w+ mode --
  562. array(1) {
  563. [0]=>
  564. string(17) "water\"fruit"\""""
  565. }
  566. int(20)
  567. bool(false)
  568. -- Testing fgetcsv() with file opened using w+b mode --
  569. array(1) {
  570. [0]=>
  571. string(17) "water\"fruit"\""""
  572. }
  573. int(20)
  574. bool(false)
  575. -- Testing fgetcsv() with file opened using w+t mode --
  576. array(1) {
  577. [0]=>
  578. string(17) "water\"fruit"\""""
  579. }
  580. int(20)
  581. bool(false)
  582. -- Testing fgetcsv() with file opened using x+ mode --
  583. array(1) {
  584. [0]=>
  585. string(17) "water\"fruit"\""""
  586. }
  587. int(20)
  588. bool(false)
  589. -- Testing fgetcsv() with file opened using x+b mode --
  590. array(1) {
  591. [0]=>
  592. string(17) "water\"fruit"\""""
  593. }
  594. int(20)
  595. bool(false)
  596. -- Testing fgetcsv() with file opened using x+t mode --
  597. array(1) {
  598. [0]=>
  599. string(17) "water\"fruit"\""""
  600. }
  601. int(20)
  602. bool(false)
  603. Done