2

我正在尝试从数据库中导出一些信息,但是当创建 CSV 文件时,特殊字符看起来像这样Categoría

这是我用来导出的代码

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;
    }

}

我一直在尝试使用 htmlspecialchars 来解决这个问题,但它不起作用,我不确定问题是我放置不正确还是其他问题......因为我不是专家我'我还是一样,有人可以帮忙吗?

4

1 回答 1

11

我可以看到您已经包含了魔术字节顺序标记(BOM)。不幸的是,它不是内容的一部分。

走线

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

并将其放在您的 readfile() 前面

...
echo "\xEF\xBB\xBF"; // UTF-8 BOM
readfile($file);
...
于 2012-04-20T17:44:48.587 回答