You are here

function ViewsDataExportExporterDelimited::add_row in Views data export 7.4

Add a single row to the export file.

Parameters

$file_handle: A PHP file handle to the export file.

array $data: An array of formatted data for this row. One cell per item.

int $row_count: The current number of rows in the export file.

$field_titles:

Overrides ViewsDataExportExporter::add_row

File

exporters/views_data_export_exporter_delimited.inc, line 133

Class

ViewsDataExportExporterDelimited
Webform exporter for creating CSV/TSV delimited files.

Code

function add_row(&$file_handle, $data, $row_count, $field_labels) {
  foreach ($data as $key => $value) {

    // Remove html tags, unless we should keep them.
    if (!isset($this->options['keep_html']) || !$this->options['keep_html']) {
      $data[$key] = strip_tags($data[$key]);
    }

    // Trim whitespace.
    if (isset($this->options['trim']) && $this->options['trim'] == true) {
      $data[$key] = trim($data[$key]);
    }

    // Replace newlines.
    if (isset($this->options['replace_newlines']) && $this->options['replace_newlines'] == true) {
      $data[$key] = str_replace("\n", $this->options['newline_replacement'], $data[$key]);
    }

    // Escape inner quotes and wrap all contents in new quotes.
    $data[$key] = '"' . str_replace('"', '""', $data[$key]) . '"';

    // Remove <script> tags, which mysteriously cause Excel not to import.
    $data[$key] = preg_replace('!<(/?script.*?)>!', '[$1]', $data[$key]);
  }
  $row = implode($this->separator, $data) . "\n";
  if (isset($this->options['encoding'])) {
    $encoding = $this->options['encoding'];

    // Blank -> UTF-8
    if ($encoding == '') {
      $row = utf8_encode($row);
    }
    else {
      if ($encoding == 'utf8_decode') {
        $row = utf8_decode($row);
      }
      else {
        $row = iconv('UTF-8', $encoding, $row);
      }
    }
  }
  else {

    // If no encoding is set, stick to UTF-8.
    $row = utf8_encode($row);
  }
  fwrite($file_handle, $row);
}