import zend standard tests
[hiphop-php.git] / hphp / test / zend / bad / ext-standard-file / fgetcsv_variation4.php
blob489ab072ddcc9e62a37212ae044ffd0accb6e1be
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 */
7 /* Testing fgetcsv() to read from a file when provided with enclosure value as NULL */
9 echo "*** Testing fgetcsv() : with enclosure as NULL ***\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
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(':', '&', '&""""&:&"&:,:":&,&:,,,,')
26 $filename = dirname(__FILE__) . '/fgetcsv_variation4.tmp';
27 @unlink($filename);
29 $file_modes = array ("r","rb", "rt", "r+", "r+b", "r+t",
30 "a+", "a+b", "a+t",
31 "w+", "w+b", "w+t",
32 "x+", "x+b", "x+t");
34 $loop_counter = 1;
35 foreach ($csv_lists as $csv_list) {
36 for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) {
37 // create the file and add the content with has csv fields
38 if ( strstr($file_modes[$mode_counter], "r") ) {
39 $file_handle = fopen($filename, "w");
40 } else {
41 $file_handle = fopen($filename, $file_modes[$mode_counter] );
43 if ( !$file_handle ) {
44 echo "Error: failed to create file $filename!\n";
45 exit();
47 $delimiter = $csv_list[0];
48 $enclosure = $csv_list[1];
49 $csv_field = $csv_list[2];
50 fwrite($file_handle, $csv_field . "\n");
51 // write another line of text and a blank line
52 // this will be used to test, if the fgetcsv() read more than a line and its
53 // working when only a blan line is read
54 fwrite($file_handle, "This is line of text without csv fields\n");
55 fwrite($file_handle, "\n"); // blank line
57 // close the file if the mode to be used is read mode and re-open using read mode
58 // else rewind the file pointer to begining of the file
59 if ( strstr($file_modes[$mode_counter], "r" ) ) {
60 fclose($file_handle);
61 $file_handle = fopen($filename, $file_modes[$mode_counter]);
62 } else {
63 // rewind the file pointer to bof
64 rewind($file_handle);
67 echo "\n-- Testing fgetcsv() with file opened using $file_modes[$mode_counter] mode --\n";
69 // call fgetcsv() to parse csv fields
71 // use length as 0
72 fseek($file_handle, 0, SEEK_SET);
73 var_dump( fgetcsv($file_handle, 0, $delimiter, NULL) );
74 // check the file pointer position and if eof
75 var_dump( ftell($file_handle) );
76 var_dump( feof($file_handle) );
78 // close the file
79 fclose($file_handle);
80 //delete file
81 unlink($filename);
82 } //end of mode loop
83 } // end of foreach
85 echo "Done\n";