Skip to content

Instantly share code, notes, and snippets.

@tormi
Forked from kostajh/xml-to-csv.php
Created March 31, 2014 23:16
Show Gist options
  • Save tormi/9904515 to your computer and use it in GitHub Desktop.
Save tormi/9904515 to your computer and use it in GitHub Desktop.
<?php
$headers = array();
foreach ($xml->ROW->children() as $field) {
$headers[] = $field->getName();
}
$csv_filename = str_replace('xml', 'csv', $filename);
$file = $this->getCsvDirectory() . '/' . $csv_filename;
if (file_exists($file)) {
unlink($file);
}
$csv = fopen($file, 'w');
fputcsv($csv, $headers, ',', '"');
foreach ($xml as $entry) {
$data = get_object_vars($entry);
// Decode HTML entities.
$sanitized_data = array();
foreach ($data as $key => $datum) {
$sanitized_data[$key] = html_entity_decode($datum, ENT_COMPAT, 'UTF-8');
}
fputcsv($csv, $sanitized_data, ',', '"');
}
fclose($csv);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment