toexport.inc.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * @version v5.20.17 31-Mar-2020
  4. * @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
  5. * @copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
  6. * Released under both BSD license and Lesser GPL library license.
  7. * Whenever there is any discrepancy between the two licenses,
  8. * the BSD license will take precedence.
  9. *
  10. * Code to export recordsets in several formats:
  11. *
  12. * AS VARIABLE
  13. * $s = rs2csv($rs); # comma-separated values
  14. * $s = rs2tab($rs); # tab delimited
  15. *
  16. * TO A FILE
  17. * $f = fopen($path,'w');
  18. * rs2csvfile($rs,$f);
  19. * fclose($f);
  20. *
  21. * TO STDOUT
  22. * rs2csvout($rs);
  23. */
  24. // returns a recordset as a csv string
  25. function rs2csv(&$rs,$addtitles=true)
  26. {
  27. return _adodb_export($rs,',',',',false,$addtitles);
  28. }
  29. // writes recordset to csv file
  30. function rs2csvfile(&$rs,$fp,$addtitles=true)
  31. {
  32. _adodb_export($rs,',',',',$fp,$addtitles);
  33. }
  34. // write recordset as csv string to stdout
  35. function rs2csvout(&$rs,$addtitles=true)
  36. {
  37. $fp = fopen('php://stdout','wb');
  38. _adodb_export($rs,',',',',true,$addtitles);
  39. fclose($fp);
  40. }
  41. function rs2tab(&$rs,$addtitles=true)
  42. {
  43. return _adodb_export($rs,"\t",',',false,$addtitles);
  44. }
  45. // to file pointer
  46. function rs2tabfile(&$rs,$fp,$addtitles=true)
  47. {
  48. _adodb_export($rs,"\t",',',$fp,$addtitles);
  49. }
  50. // to stdout
  51. function rs2tabout(&$rs,$addtitles=true)
  52. {
  53. $fp = fopen('php://stdout','wb');
  54. _adodb_export($rs,"\t",' ',true,$addtitles);
  55. if ($fp) fclose($fp);
  56. }
  57. function _adodb_export(&$rs,$sep,$sepreplace,$fp=false,$addtitles=true,$quote = '"',$escquote = '"',$replaceNewLine = ' ')
  58. {
  59. if (!$rs) return '';
  60. //----------
  61. // CONSTANTS
  62. $NEWLINE = "\r\n";
  63. $BUFLINES = 100;
  64. $escquotequote = $escquote.$quote;
  65. $s = '';
  66. if ($addtitles) {
  67. $fieldTypes = $rs->FieldTypesArray();
  68. reset($fieldTypes);
  69. $i = 0;
  70. $elements = array();
  71. foreach ($fieldTypes as $o) {
  72. $v = ($o) ? $o->name : 'Field'.($i++);
  73. if ($escquote) $v = str_replace($quote,$escquotequote,$v);
  74. $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
  75. $elements[] = $v;
  76. }
  77. $s .= implode($sep, $elements).$NEWLINE;
  78. }
  79. $hasNumIndex = isset($rs->fields[0]);
  80. $line = 0;
  81. $max = $rs->FieldCount();
  82. while (!$rs->EOF) {
  83. $elements = array();
  84. $i = 0;
  85. if ($hasNumIndex) {
  86. for ($j=0; $j < $max; $j++) {
  87. $v = $rs->fields[$j];
  88. if (!is_object($v)) $v = trim($v);
  89. else $v = 'Object';
  90. if ($escquote) $v = str_replace($quote,$escquotequote,$v);
  91. $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
  92. if (strpos($v,$sep) !== false || strpos($v,$quote) !== false) $elements[] = "$quote$v$quote";
  93. else $elements[] = $v;
  94. }
  95. } else { // ASSOCIATIVE ARRAY
  96. foreach($rs->fields as $v) {
  97. if ($escquote) $v = str_replace($quote,$escquotequote,trim($v));
  98. $v = strip_tags(str_replace("\n", $replaceNewLine, str_replace("\r\n",$replaceNewLine,str_replace($sep,$sepreplace,$v))));
  99. if (strpos($v,$sep) !== false || strpos($v,$quote) !== false) $elements[] = "$quote$v$quote";
  100. else $elements[] = $v;
  101. }
  102. }
  103. $s .= implode($sep, $elements).$NEWLINE;
  104. $rs->MoveNext();
  105. $line += 1;
  106. if ($fp && ($line % $BUFLINES) == 0) {
  107. if ($fp === true) echo $s;
  108. else fwrite($fp,$s);
  109. $s = '';
  110. }
  111. }
  112. if ($fp) {
  113. if ($fp === true) echo $s;
  114. else fwrite($fp,$s);
  115. $s = '';
  116. }
  117. return $s;
  118. }