Skip to content

Instantly share code, notes, and snippets.

@kostajh
Created March 28, 2014 16:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save kostajh/9836902 to your computer and use it in GitHub Desktop.
Save kostajh/9836902 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