/hphp/test/zend/good/ext/standard/tests/file/fgetcsv_variation8.php

https://gitlab.com/iranjith4/hhvm · PHP · 92 lines · 55 code · 13 blank · 24 comment · 6 complexity · aec3daa2ff51d6b300a27bcef7b4af29 MD5 · raw file

  1. <?php
  2. /*
  3. Prototype: array fgetcsv ( resource $handle [, int $length [, string $delimiter [, string $enclosure]]] );
  4. Description: Gets line from file pointer and parse for CSV fields
  5. */
  6. /*
  7. Testing fgetcsv() to read from a file when provided with values of delimiter and
  8. enclosure that are not present in the line read by fgetcsv()
  9. */
  10. echo "*** Testing fgetcsv() : with different delimiter and enclosure ***\n";
  11. /* the array is with three elements in it. Each element should be read as
  12. 1st element is delimiter, 2nd element is enclosure
  13. and 3rd 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&'),
  20. array('=', '=', '=water===fruit='),
  21. array('-', '-', '-water--fruit-air'),
  22. array('-', '-', '-water---fruit---air-'),
  23. array(':', '&', '&""""&:&"&:,:":&,&:,,,,')
  24. );
  25. $filename = dirname(__FILE__) . '/fgetcsv_variation8.tmp';
  26. @unlink($filename);
  27. $file_modes = array ("r","rb", "rt", "r+", "r+b", "r+t",
  28. "a+", "a+b", "a+t",
  29. "w+", "w+b", "w+t",
  30. "x+", "x+b", "x+t");
  31. $loop_counter = 1;
  32. foreach ($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. $enclosure = $csv_list[1];
  46. $csv_field = $csv_list[2];
  47. fwrite($file_handle, $csv_field . "\n");
  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. // 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. echo "\n-- Testing fgetcsv() with file opened using $file_modes[$mode_counter] mode --\n";
  63. // call fgetcsv() to parse csv fields
  64. // use different delimiter and enclosure than existing in file
  65. fseek($file_handle, 0, SEEK_SET);
  66. $del = "+";
  67. $enc = "%";
  68. var_dump( fgetcsv($file_handle, 1024, $del, $enc) );
  69. // check the file pointer position and if eof
  70. var_dump( ftell($file_handle) );
  71. var_dump( feof($file_handle) );
  72. // close the file
  73. fclose($file_handle);
  74. //delete file
  75. unlink($filename);
  76. } //end of mode loop
  77. } // end of foreach
  78. echo "Done\n";
  79. ?>