php - Save html code directly to CSV file without interpreting(Without Formatting) -
i trying save html code directly csv file. when save html formated , save csv without displaying html code:
html code trying save csv is:
<h3 style="text-align:justify">great visual</h3> <div style="line-height: 20.7999992370605px; text-align: justify;">some text text text text text text </div>
it should save html not like:
"great visual text text text text text text "
php code i'm using is:
$array = array(); $sub = array(); $sub['description_1'] = str_replace(array("\r", "\n", "\t"), "", $html1); $sub['description_2'] = str_replace(array("\r", "\n", "\t"), "", $html2); function cleandata(&$str) { $str = preg_replace("/\t/", "\\t", $str); $str = preg_replace("/\r?\n/", "\\n", $str); if (strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"'; } $name = date("f-j-y-g-i-s-a") . '-output'; $filename = $name . ".xls"; header('content-encoding: utf-8'); header("content-disposition: attachment; filename=\"$filename\""); header("content-type: application/vnd.ms-excel;charset=utf-8"); $flag = false; foreach (array_filter($array) $row) { if (!$flag) { // display field/column names first row echo implode("\t", array_keys($row)) . "\n"; $flag = true; } array_walk($row, 'cleandata'); echo implode("\t", array_values($row)) . "\n"; } exit;
please can me? tried many solutions on internet can not find working example.
simple solution. why huge , useless code using creating csv? try this.
$name = date("f-j-y-g-i-s-a") . '-output'; header("content-type: text/csv"); header("content-disposition: attachment; filename=$name.csv"); // disable caching header("cache-control: no-cache, no-store, must-revalidate"); // http 1.1 header("pragma: no-cache"); // http 1.0 header("expires: 0"); // proxies function outputcsv($data) { $output = fopen("php://output", "w"); foreach ($data $row) { fputcsv($output, $row); // here can change delimiter/enclosure } fclose($output); } outputcsv($array);
Comments
Post a Comment