Answers for "doble quotes in csv export php"

PHP
0

doble quotes in csv export php

fputs($fp, implode(",", array_map("encodeFunc", $row))."\r\n");



/***
 * @param $value array
 * @return string array values enclosed in quotes every time.
 */
function encodeFunc($value) {
    ///remove any ESCAPED double quotes within string.
    $value = str_replace('\\"','"',$value);
    //then force escape these same double quotes And Any UNESCAPED Ones.
    $value = str_replace('"','\"',$value);
    //force wrap value in quotes and return
    return '"'.$value.'"';
}

$fp = fopen("filename.csv", 'w');
foreach($table as $row){
    fputs($fp, implode(",", array_map("encodeFunc", $row))."\r\n");
}
fclose($fp);
Posted by: Guest on August-17-2021

Browse Popular Code Answers by Language