UTF8中的tabbed char

时间:2015-01-18 12:00:58

标签: php mysql utf-8 xls

我正在尝试创建我的mysql表的xls文件输出,我需要UTF8编码的文件export.php因为斯洛伐克字符 - ľčšťý...但是当我将其转换为UTF8我的脚本不能打印\ t字符

// part of export.php

header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=export.xls");
header("Pragma: no-cache");
header("Expires: 0");
$sql = ("select * from zakaznici");
$db->query("SET NAMES 'utf8'");

if (!$result = $db->query($sql))
{
    die('There was an error running the query [' . $db->error . ']');
}

// define separator (defines columns in excel & tabs in word)

$sep = "\t"; //tabbed character

// start of printing column names as names of MySQL fields

$finfo = $result->fetch_fields();

foreach($finfo as $val)
{
    printf("%s\t", $val->name);
}

print ("\n");
$schema_insert = "";

while ($ser = $result->fetch_assoc())
{
    $id = $ser['id'];
    $email = $ser['email'];
    $password = $ser['pass'];
    $schema_insert.= $id . $sep . $email . $sep . $password;
    print ("\n");
}

$schema_insert = str_replace($sep . "$", "", $schema_insert);
$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
$schema_insert.= "\t";
print (trim($schema_insert));
print "\n";

\ n字符工作正常..只有UTF8不起作用 谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

我的猜测是,生成的开头缺少UTF-8 BOM标记 文件。所以,我建议在内容输出之前添加BOM,如下所示:

print "\xEF\xBB\xBF";
print trim($schema_insert);
print "\n";

有人建议使用mb_convert_encoding(),请参阅此处:php header excel and utf-8