gpt4 book ai didi

php - 如何导出带有特殊字符的 CSV?

转载 作者:行者123 更新时间:2023-12-04 20:54:02 24 4
gpt4 key购买 nike

我正在尝试从数据库中导出一些信息,但是当创建 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 来解决这个问题,但它不起作用,我不确定问题是我放置不正确还是其他原因...因为我不是专家,我还是一样,谁能帮忙解决这个问题?

最佳答案

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

走线

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

并将它放在你的 readfile() 前面

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

关于php - 如何导出带有特殊字符的 CSV?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10250414/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com