1

I am creating a plugin in wordpress and i am trying to export data from array, it works. but the problem is with the output it print HTML also.

So please help me to get rid of this problem.

Here is my PHP code that i try

public function export_data()
{
        ob_start();
        $filename = "export_data" . rand(1, 100) . ".csv";
        $export_data = $this->order_detail_items();
        ob_end_flush();

        $output = fopen("php://output", 'w') or die("Can't open php://output");
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Content-Description: File Transfer');
        header('Content-type: text/csv');
        header("Content-Disposition: attachment; filename={$filename}");
        header('Expires: 0');
        header('Pragma: public');
        fputcsv($output, array('Store Name', 'Product name', 'Sell Quantity', 'Date', '(£) Total Price'));

        foreach ($export_data as $key => $data) {
            fputcsv($output, $data);
        }

        fclose($output);

        die();
}

enter image description here

Any solution appreciated!

1 Answers1

0

First of all I see java script tags in your result.

you can remove this tags by :

 $html = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $html);

And you can remove your html tags with strip_tags function.

$html = strip_tags($html);

At the end. you can change your code to:

foreach ($export_data as $key => $data) {
    $data = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $data);
    $data = strip_tags($data);
    fputcsv($output, $data);
}
ttrasn
  • 4,322
  • 4
  • 26
  • 43
  • But there is whole page html, start from `` to ``, how can i rid of this? –  Dec 25 '19 at 06:51
  • @RobinSingh You can remove tags with `strip_tags` function in php. which i used it in my answer – ttrasn Dec 25 '19 at 07:11