3 Prototype: array fputcsv ( resource $handle , array $fields [, string $delimiter [, string $enclosure]]] );
4 Description: Format line as CSV and write to the file pointer
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
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",
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");
47 $file_handle = fopen($filename, $file_modes[$mode_counter] );
49 if ( !$file_handle ) {
50 echo "Error: failed to create file $filename!\n";
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) );
65 // print the file contents
66 var_dump( file_get_contents($filename) );