import zend standard tests
[hiphop-php.git] / hphp / test / zend / good / ext-standard-file / fgetcsv_variation9.php
blob2daa13e3de2bfb3dec64cd7f53d0dc39d63d2f19
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 the delimiter argument value is not
9 present in the line being read by the fgetcsv()
12 echo "*** Testing fgetcsv() : with different delimiter but same enclosure ***\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_variation9.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];
53 fwrite($file_handle, $csv_field . "\n");
54 // write another line of text and a blank line
55 // this will be used to test, if the fgetcsv() read more than a line and its
56 // working when only a blan line is read
57 fwrite($file_handle, "This is line of text without csv fields\n");
58 fwrite($file_handle, "\n"); // blank line
60 // close the file if the mode to be used is read mode and re-open using read mode
61 // else rewind the file pointer to begining of the file
62 if ( strstr($file_modes[$mode_counter], "r" ) ) {
63 fclose($file_handle);
64 $file_handle = fopen($filename, $file_modes[$mode_counter]);
65 } else {
66 // rewind the file pointer to bof
67 rewind($file_handle);
70 echo "\n-- Testing fgetcsv() with file opened using $file_modes[$mode_counter] mode --\n";
72 // call fgetcsv() to parse csv fields
74 // use different delimiter but same enclosure char
75 fseek($file_handle, 0, SEEK_SET);
76 $del = "+";
77 var_dump( fgetcsv($file_handle, 1024, $del, $enclosure) );
78 // check the file pointer position and if eof
79 var_dump( ftell($file_handle) );
80 var_dump( feof($file_handle) );
82 // close the file
83 fclose($file_handle);
84 //delete file
85 unlink($filename);
86 } //end of mode loop
87 } // end of foreach
89 echo "Done\n";