TYPO3 CMS  TYPO3_6-2
toexport.inc.php
Go to the documentation of this file.
1 <?php
2 
24 // returns a recordset as a csv string
25 function rs2csv(&$rs,$addtitles=true)
26 {
27  return _adodb_export($rs,',',',',false,$addtitles);
28 }
29 
30 // writes recordset to csv file
31 function rs2csvfile(&$rs,$fp,$addtitles=true)
32 {
33  _adodb_export($rs,',',',',$fp,$addtitles);
34 }
35 
36 // write recordset as csv string to stdout
37 function rs2csvout(&$rs,$addtitles=true)
38 {
39  $fp = fopen('php://stdout','wb');
40  _adodb_export($rs,',',',',true,$addtitles);
41  fclose($fp);
42 }
43 
44 function rs2tab(&$rs,$addtitles=true)
45 {
46  return _adodb_export($rs,"\t",',',false,$addtitles);
47 }
48 
49 // to file pointer
50 function rs2tabfile(&$rs,$fp,$addtitles=true)
51 {
52  _adodb_export($rs,"\t",',',$fp,$addtitles);
53 }
54 
55 // to stdout
56 function rs2tabout(&$rs,$addtitles=true)
57 {
58  $fp = fopen('php://stdout','wb');
59  _adodb_export($rs,"\t",' ',true,$addtitles);
60  if ($fp) fclose($fp);
61 }
62 
63 function _adodb_export(&$rs,$sep,$sepreplace,$fp=false,$addtitles=true,$quote = '"',$escquote = '"',$replaceNewLine = ' ')
64 {
65  if (!$rs) return '';
66  //----------
67  // CONSTANTS
68  $NEWLINE = "\r\n";
69  $BUFLINES = 100;
70  $escquotequote = $escquote.$quote;
71  $s = '';
72 
73  if ($addtitles) {
74  $fieldTypes = $rs->FieldTypesArray();
75  reset($fieldTypes);
76  $i = 0;
77  while(list(,$o) = each($fieldTypes)) {
78 
79  $v = ($o) ? $o->name : 'Field'.($i++);
80  if ($escquote) $v = str_replace($quote,$escquotequote,$v);
81  $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
82  $elements[] = $v;
83 
84  }
85  $s .= implode($sep, $elements).$NEWLINE;
86  }
87  $hasNumIndex = isset($rs->fields[0]);
88 
89  $line = 0;
90  $max = $rs->FieldCount();
91 
92  while (!$rs->EOF) {
93  $elements = array();
94  $i = 0;
95 
96  if ($hasNumIndex) {
97  for ($j=0; $j < $max; $j++) {
98  $v = $rs->fields[$j];
99  if (!is_object($v)) $v = trim($v);
100  else $v = 'Object';
101  if ($escquote) $v = str_replace($quote,$escquotequote,$v);
102  $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
103 
104  if (strpos($v,$sep) !== false || strpos($v,$quote) !== false) $elements[] = "$quote$v$quote";
105  else $elements[] = $v;
106  }
107  } else { // ASSOCIATIVE ARRAY
108  foreach($rs->fields as $v) {
109  if ($escquote) $v = str_replace($quote,$escquotequote,trim($v));
110  $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
111 
112  if (strpos($v,$sep) !== false || strpos($v,$quote) !== false) $elements[] = "$quote$v$quote";
113  else $elements[] = $v;
114  }
115  }
116  $s .= implode($sep, $elements).$NEWLINE;
117  $rs->MoveNext();
118  $line += 1;
119  if ($fp && ($line % $BUFLINES) == 0) {
120  if ($fp === true) echo $s;
121  else fwrite($fp,$s);
122  $s = '';
123  }
124  }
125 
126  if ($fp) {
127  if ($fp === true) echo $s;
128  else fwrite($fp,$s);
129  $s = '';
130  }
131 
132  return $s;
133 }
rs2csv(&$rs, $addtitles=true)
if(isset($_REQUEST['nrows'])) else $rs
Definition: server.php:92
_adodb_export(&$rs, $sep, $sepreplace, $fp=false, $addtitles=true, $quote='"',$escquote = '"', $replaceNewLine=' ')
$sep
Definition: server.php:42
rs2csvout(&$rs, $addtitles=true)
rs2csvfile(&$rs, $fp, $addtitles=true)
rs2tabout(&$rs, $addtitles=true)
rs2tabfile(&$rs, $fp, $addtitles=true)
rs2tab(&$rs, $addtitles=true)