0

I have an array where one of the items is an string, I need to download that array through the web navigator as a text file (.txt) but the string item is having quotes in the text file. It is presented as 0000362367 "00100 E0.1.1" I would like to have the result without the quotes: 0000362367 00100 E0.1.1

Thanks in advance!

This is my PHP code:

                   $ar_result=array();
                    $ar=array();

                    $ar['u_id']= str_pad('362367', 10, "0", STR_PAD_LEFT);
                    $ar['NODO']='00100 E0.1.1'; 
                    $ar_result[]=$ar;

                    $ar['u_id']= str_pad('4352746', 10, "0", STR_PAD_LEFT);
                    $ar['NODO']='00100 E0.1.1'; 
                    $ar_result[]=$ar;



                    $filename = "test.txt";
                    $delimiter=" ";
                    header('Content-Type: application/txt');
                    header('Content-Disposition: attachment; filename="'.$filename.'";');
                    $handle = fopen('php://output', 'w');
                    foreach ($ar_result as $line) {
                       stream_filter_register("newlines", "StreamFilterNewlines");
                       stream_filter_append($handle, "newlines");
                        fputcsv($handle, $line, $delimiter);
                    }



class StreamFilterNewlines extends php_user_filter {
    function filter($in, $out, &$consumed, $closing) {

        while ( $bucket = stream_bucket_make_writeable($in) ) {
            $bucket->data = preg_replace('/([^\r])\n/', "$1\r\n", $bucket->data);
            $consumed += $bucket->datalen;
            stream_bucket_append($out, $bucket);
        }
        return PSFS_PASS_ON;
    }
}

  • 2
    There's no way to prevent fputcsv from quoting strings with spaces. If you don't want the quotes, just use implode and write that to the file. See https://stackoverflow.com/questions/1800675/write-csv-to-file-without-enclosures-in-php – Don't Panic Apr 23 '19 at 23:08
  • Yeah! It is working now using this: fputcsv($f,$array,',',chr(0)); Thanks @Don'tPanic for sharing the link to the answer! It is fixed. – Fernando Rivero Apr 23 '19 at 23:37

0 Answers0