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