import zend standard tests
[hiphop-php.git] / hphp / test / zend / good / ext-standard-file / fputcsv_variation7.php
blob04e0fc43439677aa352baa5add2843ad6264c8e5
1 <?php
2 /*
3 Prototype: array fputcsv ( resource $handle , array $fields [, string $delimiter [, string $enclosure]]] );
4 Description: Format line as CSV and write to the file pointer
5 */
7 /*
8 Testing fputcsv() to write to a file when enclosure is same but delimiter is different from those
9 present in the field to be written to the file
12 echo "*** Testing fputcsv() : with different delimiter and 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(',', '"', array('water','fruit') ),
20 array(',', '"', array('"water","fruit') ),
21 array(',', '"', array('"water","fruit"') ),
22 array(' ', '^', array('^water^ ^fruit^')),
23 array(':', '&', array('&water&:&fruit&')),
24 array('=', '=', array('=water===fruit=')),
25 array('-', '-', array('-water--fruit-air')),
26 array('-', '-', array('-water---fruit---air-')),
27 array(':', '&', array('&""""&:&"&:,:":&,&:,,,,'))
30 $file_path = dirname(__FILE__);
31 $filename = "$file_path/fputcsv_variation7.tmp";
33 $file_modes = array ("r+", "r+b", "r+t",
34 "a+", "a+b", "a+t",
35 "w+", "w+b", "w+t",
36 "x+", "x+b", "x+t");
38 $loop_counter = 1;
39 foreach ($csv_lists as $csv_list) {
40 for($mode_counter = 0; $mode_counter < count($file_modes); $mode_counter++) {
42 echo "\n-- file opened in $file_modes[$mode_counter] --\n";
43 // create the file and add the content with has csv fields
44 if ( strstr($file_modes[$mode_counter], "r") ) {
45 $file_handle = fopen($filename, "w");
46 } else {
47 $file_handle = fopen($filename, $file_modes[$mode_counter] );
49 if ( !$file_handle ) {
50 echo "Error: failed to create file $filename!\n";
51 exit();
53 $delimiter = $csv_list[0];
54 $enclosure = $csv_list[1];
55 $csv_field = $csv_list[2];
57 // write to a file in csv format
58 var_dump( fputcsv($file_handle, $csv_field, '+', $enclosure) );
59 // check the file pointer position and eof
60 var_dump( ftell($file_handle) );
61 var_dump( feof($file_handle) );
62 //close the file
63 fclose($file_handle);
65 // print the file contents
66 var_dump( file_get_contents($filename) );
68 //delete file
69 unlink($filename);
70 } //end of mode loop
71 } // end of foreach
73 echo "Done\n";