使用php创建csv文件时的希伯来语问题

使用php创建csv文件时的希伯来语问题,php,csv,utf-8,export-to-csv,hebrew,Php,Csv,Utf 8,Export To Csv,Hebrew,我有以下代码: // output headers so that the file is downloaded rather than displayed header('Content-Type: text/csv; charset=utf-8'); header('Content-Disposition: attachment; filename=data.csv'); // create a file pointer connected to the output stream $ou

我有以下代码:

// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');

// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');

// output the column headings
fputcsv($output, array('שדה 1', 'שדה 2', 'שדה 3'));

include('config.php');
$id = 2;
$sql = "SELECT name FROM shoes WHERE event_id = '$id'";
$result = mysqli_query($connection,$sql);

// loop over the rows, outputting them
while ($row = mysqli_fetch_assoc($result)) fputcsv($output, $row);
第一行没问题,我得到“1”、“2”、“3”但我从数据库得到的信息是这样的:“׳”“׳•׳”“׳§”

为什么不是希伯来语

你可以在这里找到它:

这里是我的配置文件:

defined('DB_HOST')? null : define('DB_HOST', 'localhost');
defined('DB_USER')?  null : define('DB_USER', '***');
defined('DB_PASS')?  null : define('DB_PASS', '***');
defined('DB_NAME')?  null : define('DB_NAME', '***');

$connection = mysqli_connect(DB_HOST ,DB_USER ,DB_PASS ,DB_NAME);

if (mysqli_connect_errno($connection)){
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    die();
}

mysqli_set_charset($connection, "utf8");
如您所见,我使用“mysqli\u set\u charset”。 此外,我的all db排序规则是utf8\u general\u ci。 在我的其他php文件中,细节很好(来自数据库)


谢谢。

我从您的网站下载了CSV。这非常有用-如果所有人在提问时都这样做的话:)

检查表明,第一行是希伯来文/Windows-1255编码,第二行是UTF-8编码。使用合适的文本编辑器(如记事本+)查看文件,并尝试在编码类型之间切换。十六进制编辑器证实了我的怀疑

这意味着您的源代码是用8位希伯来语编码编写的,而您的数据库正确地包含UTF-8字符。这两个字符串都会逐字返回到客户端

第一行看起来是正确的,因为您用来查看.csv的内容也在Windows-1255模式下运行。我怀疑您使用的是Excel,这可以解释结果

由于您不能保证每个人都在使用Windows-1255/希伯来语代码页,因此最好将源代码更改为UTF-8编码。这意味着,源代码中字符的非ASCII输出将显示为UTF-8

如果您使用的是Excel,您现在会遇到告诉Excel将文件视为UTF-8的问题。UTF-8字节顺序标记可以解决此问题。它允许Excel工作,但会收到警告-并非所有纯文本编辑器都理解它

要添加UTF-8 BOM表,请执行以下操作:

// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');

// UTF-8 BOM
fwrite($output, "\xEF\xBB\xBF");

// output the column headings
fputcsv($output, array('שדה 1', 'שדה 2', 'שדה 3'));
见以下条款:


感谢您的评论,我已经尝试在我的文件中添加这一行,没有任何不同。我想我没有理解您的解决方案,该怎么办?我尝试将unicode更改为utf-8,但没有bom,但我得到了“headers allready sent…”。我该怎么办?感谢并为我的英语感到抱歉!您更改了源文件的编码吗?@DorBenZaken-您的英语很棒-无需道歉!我相信您最近出现错误的原因是因为我建议在使用原始输出时使用
echo
。我已更新了答案,以便在中写入文件描述符stead,告诉我这是否有效。