Skip to content
This repository has been archived by the owner on Jun 6, 2021. It is now read-only.

Update php-export-data.class.php #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions php-export-data.class.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
<?php
// php-export-data by Eli Dickinson, http://github.com/elidickinson/php-export-data

if (!function_exists('mb_str_replace')) {
function mb_str_replace($search, $replace, $subject, &$count = 0) {
if (!is_array($subject)) {
// Normalize $search and $replace so they are both arrays of the same length
$searches = is_array($search) ? array_values($search) : array($search);
$replacements = is_array($replace) ? array_values($replace) : array($replace);
$replacements = array_pad($replacements, count($searches), '');
foreach ($searches as $key => $search) {
$parts = mb_split(preg_quote($search), $subject);
$count += count($parts) - 1;
$subject = implode($replacements[$key], $parts);
}
} else {
// Call mb_str_replace for each subject in array, recursively
foreach ($subject as $key => $value) {
$subject[$key] = mb_str_replace($search, $replace, $value, $count);
}
}
return $subject;
}
}
/**
* ExportData is the base class for exporters to specific file formats. See other
* classes below.
Expand Down Expand Up @@ -43,6 +63,14 @@ public function addRow($row) {
$this->write($this->generateRow($row));
}

public function addRowToArray($allRows = array()) {
if(is_array($allRows) && count($allRows)>=1){
foreach ($allRows as $row){
$this->write($this->generateRow($row));
}
}
}

public function finalize() {

$this->write($this->generateFooter());
Expand Down Expand Up @@ -104,7 +132,8 @@ function generateRow($row) {
foreach ($row as $key => $value) {
// Escape inner quotes and wrap all contents in new quotes.
// Note that we are using \" to escape double quote not ""
$row[$key] = '"'. str_replace('"', '\"', $value) .'"';
//$row[$key] = '"'. str_replace('"', '\"', $value) .'"';
$row[$key] = '"'. mb_str_replace('"', '\"', $value) .'"';
}
return implode("\t", $row) . "\n";
}
Expand All @@ -124,7 +153,8 @@ function generateRow($row) {
foreach ($row as $key => $value) {
// Escape inner quotes and wrap all contents in new quotes.
// Note that we are using \" to escape double quote not ""
$row[$key] = '"'. str_replace('"', '\"', $value) .'"';
//$row[$key] = '"'. str_replace('"', '\"', $value) .'"';
$row[$key] = '"'. mb_str_replace('"', '\"', $value) .'"';
}
return implode(",", $row) . "\n";
}
Expand Down Expand Up @@ -239,4 +269,4 @@ function sendHttpHeaders() {
header("Content-Disposition: inline; filename=\"" . basename($this->filename) . "\"");
}

}
}