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!

4
  • Can you show us sample of html ? Commented Dec 25, 2019 at 6:17
  • @ttrasn sure, i will show you, Please check the updated post Commented Dec 25, 2019 at 6:26
  • actually i didnt found html on your csv. can you tell me where is it? or can you take image ? Commented Dec 25, 2019 at 6:40
  • @ttrasn please check, now you can see the image Commented Dec 25, 2019 at 6:43

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

But there is whole page html, start from <html> to </html>, how can i rid of this?
@RobinSingh You can remove tags with strip_tags function in php. which i used it in my answer