Remove QUOTES when generating TXT file using PHP

Asked

Viewed 598 times

2

How to remove "double quotes" in TXT file extracted from mysqli with PHP.

PHP function to create file

// Export data in CSV format
    public function exportCSV(){
        $id = $this->input->get("id");

        // file name
        $filename = 'Avm_inventarios_'.date('d M Y H:i:s').'.txt';
        header("Content-Description: File Transfer");
        header("Content-Disposition: attachment; filename=$filename");
        header("Content-Type: application/txt");

        // get data
        $dados_db = $this->Inventarios_model->gerar_txt($id);
        // file creation
        $file = fopen('php://output', 'W');
        $header = array();

        fputcsv($file,$header);

        foreach ($dados_db as $line){
            //print_r($line);
        fputcsv ($file,str_replace('"','',$line),"\n");
            }
        fclose($file);
        exit;
    }

database

Dados do banco TXT file generated

Arquivo TXT gerado

  • Quotes are not being generated by the method $this->Inventarios_model->gerar_txt($id);?

  • No. printed data is without Quotation marks.

  • Have you tried using the fourth parameter as empty? So: fputcsv($file,str_replace('"','',$line),"\n",'');

  • using this way got the following error: Message: fputcsv(): enclosure must be a character fputcsv($file,str_replace('"','',$line),"\n",'');, however it does not bring all the spaces that exist in the table The Archive looks like this 163836353095 00001 when you should be like this 163836353095 00001

  • Hugo, do you have access to the database? You can transform the column where it is 00001 to just 1? I think that’s the option ZEROFILL. Thus, the fputcsv will not put the quotes. I think we are trying to get around the variable problem $line but who is putting the quotes is himself fputcsv.

2 answers

2

To clear the values extracted from the foreach array try to replace the str_replace() for preg_replace() with the following regular expression:

'/[^0-9]/' - Characters from 0 to 9

'/[^0-9 ]/' - Characters from 0 to 9 and blanks

The function will remove everything that is not in accordance with the regular expression.

Your code will stay:

// Export data in CSV format
    public function exportCSV(){
        $id = $this->input->get("id");

        // file name
        $filename = 'Avm_inventarios_'.date('d M Y H:i:s').'.txt';
        header("Content-Description: File Transfer");
        header("Content-Disposition: attachment; filename=$filename");
        header("Content-Type: application/txt");

        // get data
        $dados_db = $this->Inventarios_model->gerar_txt($id);
        // file creation
        $file = fopen('php://output', 'W');

        foreach ($dados_db as $line){
            fwrite($file, preg_replace('/[^0-9 ]/', '', $line)."\r\n");
        }

        fclose($file);
        exit;
    }

I changed the function fputcsv() (because the goal is to create a file without separators) for the function fwrite().

  • Using the function preg_replace() really would be the most appropriate method because I need to bring only numbers. but also need to bring space before zeroes and thus eliminated empty spaces as well. Using the function str_replace('"', '', $line) does not have effect with double Apas. If it were to print directly in HTML it would work, but when generates in TXT goes with quotation marks.

  • And so: fputcsv($file, preg_replace('/\"/', '', $line),"\n"); ?

  • It also has quotes 163836353095 " 00001"

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.