3 * Export recordsets in several formats.
6 * $s = rs2csv($rs); # comma-separated values
7 * $s = rs2tab($rs); # tab delimited
10 * $f = fopen($path,'w');
17 * This file is part of ADOdb, a Database Abstraction Layer library for PHP.
20 * @link https://adodb.org Project's web site and documentation
21 * @link https://github.com/ADOdb/ADOdb Source code and issue tracker
23 * The ADOdb Library is dual-licensed, released under both the BSD 3-Clause
24 * and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option,
25 * any later version. This means you can use it in proprietary products.
26 * See the LICENSE.md file distributed with this source code for details.
27 * @license BSD-3-Clause
28 * @license LGPL-2.1-or-later
30 * @copyright 2000-2013 John Lim
31 * @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
34 // returns a recordset as a csv string
35 function rs2csv(&$rs,$addtitles=true)
37 return _adodb_export($rs,',',',',false,$addtitles);
40 // writes recordset to csv file
41 function rs2csvfile(&$rs,$fp,$addtitles=true)
43 _adodb_export($rs,',',',',$fp,$addtitles);
46 // write recordset as csv string to stdout
47 function rs2csvout(&$rs,$addtitles=true)
49 $fp = fopen('php://stdout','wb');
50 _adodb_export($rs,',',',',true,$addtitles);
54 function rs2tab(&$rs,$addtitles=true)
56 return _adodb_export($rs,"\t",',',false,$addtitles);
60 function rs2tabfile(&$rs,$fp,$addtitles=true)
62 _adodb_export($rs,"\t",',',$fp,$addtitles);
66 function rs2tabout(&$rs,$addtitles=true)
68 $fp = fopen('php://stdout','wb');
69 _adodb_export($rs,"\t",' ',true,$addtitles);
73 function _adodb_export(&$rs,$sep,$sepreplace,$fp=false,$addtitles=true,$quote = '"',$escquote = '"',$replaceNewLine = ' ')
80 $escquotequote = $escquote.$quote;
84 $fieldTypes = $rs->FieldTypesArray();
88 foreach ($fieldTypes as $o) {
90 $v = ($o) ?
$o->name
: 'Field'.($i++
);
91 if ($escquote) $v = str_replace($quote,$escquotequote,$v);
92 $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
96 $s .= implode($sep, $elements).$NEWLINE;
98 $hasNumIndex = isset($rs->fields
[0]);
101 $max = $rs->FieldCount();
108 for ($j=0; $j < $max; $j++
) {
109 $v = $rs->fields
[$j];
110 if (!is_object($v)) $v = trim($v);
112 if ($escquote) $v = str_replace($quote,$escquotequote,$v);
113 $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
115 if (strpos($v,$sep) !== false ||
strpos($v,$quote) !== false) $elements[] = "$quote$v$quote";
116 else $elements[] = $v;
118 } else { // ASSOCIATIVE ARRAY
119 foreach($rs->fields
as $v) {
120 if ($escquote) $v = str_replace($quote,$escquotequote,trim($v));
121 $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
123 if (strpos($v,$sep) !== false ||
strpos($v,$quote) !== false) $elements[] = "$quote$v$quote";
124 else $elements[] = $v;
127 $s .= implode($sep, $elements).$NEWLINE;
130 if ($fp && ($line %
$BUFLINES) == 0) {
131 if ($fp === true) echo $s;
138 if ($fp === true) echo $s;