/** * Convert CSV to XML * * Function ported over from List Attachments Shortcode plugin. * * @version 1.32.0 */ public static function csv_to_xml($string, $args = false) { $uploads = wp_upload_dir(); $defaults = array('delimiter' => ',', 'enclosure' => '"', 'escape' => "\\"); extract(wp_parse_args($args, $defaults), EXTR_SKIP); $temp_file = $uploads['path'] . time() . '.csv'; file_put_contents($temp_file, $string); ini_set("auto_detect_line_endings", 1); $current_row = 1; $handle = fopen($temp_file, "r"); $header_array = array(); $csv = array(); while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) { $number_of_fields = count($data); if ($current_row == 1) { for ($c = 0; $c < $number_of_fields; $c++) { $header_array[$c] = str_ireplace('-', '_', sanitize_key($data[$c])); } } else { $data_array = array(); for ($c = 0; $c < $number_of_fields; $c++) { //** Clean up values */ $value = trim($data[$c]); $data_array[$header_array[$c]] = $value; } /** Removing - this removes empty values from the CSV, we want to leave them to make sure the associative array is consistant for the importer - $data_array = array_filter($data_array); */ if (!empty($data_array)) { $csv[] = $data_array; } } $current_row++; } fclose($handle); unlink($temp_file); //** Get it into XML (We want to use json_to_xml because it does all the cleansing of weird characters) */ $xml = WPP_F::json_to_xml(json_encode($csv)); return $xml; }