import zend standard tests
[hiphop-php.git] / hphp / test / zend / bad / ext-standard-file / fgetcsv_variation25.php
blob5051b5d5e3deceaf3dda5ffa420b2264ba796f81
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 /*
8 Testing fgetcsv() to read from a file when provided with negative length argument
9 along with delimiter and enclosure arguments
12 echo "*** Testing fgetcsv() : with negative length value ***\n";
14 /* the array is with three elements in it. Each element should be read as
15 1st element is delimiter, 2nd element is enclosure
16 and 3rd element is csv fields
18 $csv_lists = array (
19 array(',', '"', '"water",fruit'),
20 array(',', '"', '"water","fruit"'),
21 array(' ', '^', '^water^ ^fruit^'),
22 array(':', '&', '&water&:&fruit&'),
23 array('=', '=', '=water===fruit='),
24 array('-', '-', '-water--fruit-air'),
25 array('-', '-', '-water---fruit---air-'),
26 array(':', '&', '&""""&:&"&:,:":&,&:,,,,')
29 $filename = dirname(__FILE__) . '/fgetcsv_variation25.tmp';
30 @unlink($filename);
32 $file_modes = array ("r","rb", "rt", "r+", "r+b", "r+t",
33 "a+", "a+b", "a+t",
34 "w+", "w+b", "w+t",
35 "x+", "x+b", "x+t");
37 $loop_counter = 1;
38 foreach ($csv_lists as $csv_list) {
39 for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) {
40 // create the file and add the content with has csv fields
41 if ( strstr($file_modes[$mode_counter], "r") ) {
42 $file_handle = fopen($filename, "w");
43 } else {
44 $file_handle = fopen($filename, $file_modes[$mode_counter] );
46 if ( !$file_handle ) {
47 echo "Error: failed to create file $filename!\n";
48 exit();
50 $delimiter = $csv_list[0];
51 $enclosure = $csv_list[1];
52 $csv_field = $csv_list[2];
54 fwrite($file_handle, $csv_field . "\n");
55 // write another line of text and a blank line
56 // this will be used to test, if the fgetcsv() read more than a line and its
57 // working when only a blank line is read
58 fwrite($file_handle, "This is line of text without csv fields\n");
59 fwrite($file_handle, "\n"); // blank line
61 // close the file if the mode to be used is read mode and re-open using read mode
62 // else rewind the file pointer to begining of the file
63 if ( strstr($file_modes[$mode_counter], "r" ) ) {
64 fclose($file_handle);
65 $file_handle = fopen($filename, $file_modes[$mode_counter]);
66 } else {
67 // rewind the file pointer to bof
68 rewind($file_handle);
71 echo "\n-- Testing fgetcsv() with file opened using $file_modes[$mode_counter] mode --\n";
73 // call fgetcsv() to parse csv fields
75 // use the right delimiter and enclosure with negative length
76 var_dump( fgetcsv($file_handle, -10, $delimiter, $enclosure) );
77 // check the file pointer position and if eof
78 var_dump( ftell($file_handle) );
79 var_dump( feof($file_handle) );
81 // close the file
82 fclose($file_handle);
83 //delete file
84 unlink($filename);
85 } //end of mode loop
86 } // end of foreach
88 echo "Done\n";