00001 <?php
00002
00024
00025 function rs2csv(&$rs,$addtitles=true)
00026 {
00027 return _adodb_export($rs,',',',',false,$addtitles);
00028 }
00029
00030
00031 function rs2csvfile(&$rs,$fp,$addtitles=true)
00032 {
00033 _adodb_export($rs,',',',',$fp,$addtitles);
00034 }
00035
00036
00037 function rs2csvout(&$rs,$addtitles=true)
00038 {
00039 $fp = fopen('php:
00040 _adodb_export($rs,',',',',true,$addtitles);
00041 fclose($fp);
00042 }
00043
00044 function rs2tab(&$rs,$addtitles=true)
00045 {
00046 return _adodb_export($rs,"\t",',',false,$addtitles);
00047 }
00048
00049
00050 function rs2tabfile(&$rs,$fp,$addtitles=true)
00051 {
00052 _adodb_export($rs,"\t",',',$fp,$addtitles);
00053 }
00054
00055
00056 function rs2tabout(&$rs,$addtitles=true)
00057 {
00058 $fp = fopen('php:
00059 _adodb_export($rs,"\t",' ',true,$addtitles);
00060 if ($fp) fclose($fp);
00061 }
00062
00063 function _adodb_export(&$rs,$sep,$sepreplace,$fp=false,$addtitles=true,$quote = '"',$escquote = '"',$replaceNewLine = ' ')
00064 {
00065 if (!$rs) return '';
00066
00067
00068 $NEWLINE = "\r\n";
00069 $BUFLINES = 100;
00070 $escquotequote = $escquote.$quote;
00071 $s = '';
00072
00073 if ($addtitles) {
00074 $fieldTypes = $rs->FieldTypesArray();
00075 reset($fieldTypes);
00076 while(list(,$o) = each($fieldTypes)) {
00077
00078 $v = $o->name;
00079 if ($escquote) $v = str_replace($quote,$escquotequote,$v);
00080 $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
00081 $elements[] = $v;
00082
00083 }
00084 $s .= implode($sep, $elements).$NEWLINE;
00085 }
00086 $hasNumIndex = isset($rs->fields[0]);
00087
00088 $line = 0;
00089 $max = $rs->FieldCount();
00090
00091 while (!$rs->EOF) {
00092 $elements = array();
00093 $i = 0;
00094
00095 if ($hasNumIndex) {
00096 for ($j=0; $j < $max; $j++) {
00097 $v = $rs->fields[$j];
00098 if (!is_object($v)) $v = trim($v);
00099 else $v = 'Object';
00100 if ($escquote) $v = str_replace($quote,$escquotequote,$v);
00101 $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
00102
00103 if (strpos($v,$sep) !== false || strpos($v,$quote) !== false) $elements[] = "$quote$v$quote";
00104 else $elements[] = $v;
00105 }
00106 } else {
00107 foreach($rs->fields as $v) {
00108 if ($escquote) $v = str_replace($quote,$escquotequote,trim($v));
00109 $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
00110
00111 if (strpos($v,$sep) !== false || strpos($v,$quote) !== false) $elements[] = "$quote$v$quote";
00112 else $elements[] = $v;
00113 }
00114 }
00115 $s .= implode($sep, $elements).$NEWLINE;
00116 $rs->MoveNext();
00117 $line += 1;
00118 if ($fp && ($line % $BUFLINES) == 0) {
00119 if ($fp === true) echo $s;
00120 else fwrite($fp,$s);
00121 $s = '';
00122 }
00123 }
00124
00125 if ($fp) {
00126 if ($fp === true) echo $s;
00127 else fwrite($fp,$s);
00128 $s = '';
00129 }
00130
00131 return $s;
00132 }
00133 ?>