Gman
Gman

Reputation: 783

How can I export CSV with special characters?

I'm trying to export some info from the database but when the CSV file is created the special characters look like this Categoría

This is the code i'm using to export

function exportemail() {
global $wpdb;
    $content = "No.,Nombre,Empresa,Categoría,Estado de Prospecto,Correo,Teléfono,Dirección,Referido por,País,Sitio web,Contacto Alternativo,Trabajo de Contacto Alternativo,Correo de Contacto Alternativo,Teléfono de Contacto Alternativo,Dirección de Contacto Alternativo,País de Contacto Alternativo,Sitio Web de Contacto Alternativo,Notas,Rung";

    if(trim($_GET['tab']) != "")
    {
        $sql = "SELECT * FROM ".$wpdb->prefix."crm WHERE
              category LIKE '%".$wpdb->escape($_GET['tab'])."%'
              ORDER BY name";
    }
   elseif (trim($searcharea) != '') {
        $sql = "SELECT * FROM ".$wpdb->prefix."crm WHERE
            name LIKE '%".$wpdb->escape($_GET['q'])."%'
            OR last_name LIKE '%".$wpdb->escape($_GET['q'])."%'
            OR category LIKE '%".$wpdb->escape($_GET['q'])."%'
            OR business LIKE '%".$wpdb->escape($_GET['q'])."%'
            OR email LIKE '%".$wpdb->escape($_GET['q'])."%'
            OR phone LIKE '%".$wpdb->escape($_GET['q'])."%'
            OR notes LIKE '%".$wpdb->escape($_GET['q'])."%'
            OR rung LIKE '%".$wpdb->escape($_GET['q'])."%'
            ORDER BY name";
    } else {
        $sql = "SELECT * FROM ".$wpdb->prefix."crm ORDER BY name";
    }

    $results = $wpdb->get_results($sql);
        $c = 1;
        foreach ($results as $row) {


        $content .= "\n".$c.",".str_replace(","," -",
        $row->name." ".
        $row->last_name).",".str_replace(","," -",
        $row->business).",".str_replace(","," -",
        $row->category).",".str_replace(","," -",
        $row->rank).",".str_replace(","," -",
        $row->email).",".str_replace(","," -",
        $row->phone).",".str_replace(","," -",
        $row->address).",".str_replace(","," -",
        $row->referral).",".str_replace(","," -",
        $row->country).",".str_replace(","," -",
        $row->website).",".str_replace(","," -",
        $row->altern_last_name." ".
        $row->altern_last_name).",".str_replace(","," -",
        $row->altern_business).",".str_replace(","," -",
        $row->altern_email).",".str_replace(","," -",
        $row->altern_phone).",".str_replace(","," -",
        $row->altern_address)." ".str_replace(","," -",
        $row->altern_country).",".str_replace(","," -",
        $row->altern_website).",".str_replace(","," -",
        $row->notes).",".str_replace(","," -",
        $row->rung);

        $c++;
    }


    $file = "../wp-content/plugins/email.csv";
    chmod($file,0777);
    $fp = fopen($file,'w');
    fwrite($fp,$content);
    fclose($fp);
    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Encoding: UTF-8');
        header('Content-type: text/csv; charset=UTF-8');
        header('Content-Disposition: attachment; filename='.basename($file));
        echo "\xEF\xBB\xBF"; // UTF-8 BOM
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        unlink($file);
        exit;
    }

}

i've been trying to use htmlspecialchars to solve this problem but it's not working and i'm not sure if the problem is that i'm placing it incorrectly or if it's something else... since i'm not an expert i'm still getting the same, Can anyone help with this one?

Upvotes: 2

Views: 6570

Answers (1)

Jens Bradler
Jens Bradler

Reputation: 1497

I can see that you already have included the magic byte order mark (BOM). Unfortunately it's not part of the content.

Take the line

echo "\xEF\xBB\xBF"; // UTF-8 BOM

and put it just in front of your readfile()

...
echo "\xEF\xBB\xBF"; // UTF-8 BOM
readfile($file);
...

Upvotes: 11

Related Questions