Skip to content

Instantly share code, notes, and snippets.

@encoderit-arman
Created August 11, 2022 08:33
Show Gist options
  • Save encoderit-arman/783e1b641abd7eb2cc9df98464e38a3d to your computer and use it in GitHub Desktop.
Save encoderit-arman/783e1b641abd7eb2cc9df98464e38a3d to your computer and use it in GitHub Desktop.
$fileName = "contact-list-" . date('Y-m-d') . ".xls";
ob_end_clean();
$fields = array('ID', 'Name', 'Email', 'Phone', 'Request For', 'Job Address', 'Work Type', 'About Job', 'Created At');
// $output = fopen('php://output', 'w');
// fputcsv($output, $fields);
$excelData = implode("\t", array_values($fields)) . "\n";
function filterData(&$str){
$str = preg_replace("/\t/", "\\t", $str);
$str = preg_replace("/\r?\n/", "\\n", $str);
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
}
foreach ($all_data as $single_data) {
$lineData = array($single_data['id'], $single_data['cus_fname'], $single_data['cus_email'], $single_data['cus_phone'], $single_data['cus_request_for'], $single_data['cus_job_address'], $single_data['cus_work_type'], $single_data['cus_about_job'], $single_data['created_at']);
array_walk($lineData, 'filterData');
$excelData .= implode("\t", array_values($lineData)) . "\n";
}
header('Content-Type: text/csv; charset=utf-8');
header("Content-Type:application/octet-stream");
header('Content-Disposition: attachment; filename=' . $fileName);
// fclose($output);
echo $excelData;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment