0

好吧,几个月来我一直在努力解决这个问题。我有一个遗留的 MySQL 数据库,可以描述为字符地狱。经过大量工作,我能够修复所有显示不正确的字符并将所有内容都设为 UTF-8(表格、排序规则、与数据库的连接、标题等)。

现在的问题是,当我尝试使用 PHPWord 创建 Word 文档时,双引号(“)显示为它的 html 实体等效项"。这是我胜利之间的唯一障碍,所以,如果有任何想法,我会非常感谢.

这是代码:

include_once "../PHPWord.php";
include_once "../debug.php";

$file_name = "filename.docx";
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file_name");
//header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");
header("Content-Type: application/vnd.ms-word; charset=utf-8");
header("Content-Transfer-Encoding: binary");

$index = 1218;
$mysqli = new mysqli("host","user","pass","database");
$mysqli->set_charset("utf8");
$qry1 = "SELECT * FROM table WHERE index = $index ORDER BY field DESC";
$qry2 = "SELECT * FROM table2 WHERE index = $index";

$PHPWord = new PHPWord();
$section = $PHPWord->createSection();

$section->addText("Connection Encoding: ".$mysqli->character_set_name());
$section->addText("resuls");

$res1 = $mysqli->query($qry1);
while($row = $res1->fetch_assoc()){
    $section->addText($row[name]);
    $section->addText(mb_detect_encoding($row['name']));
}

$section->addText("results2");

$res2 = $mysqli->query($qry2);
while($row = $res2->fetch_assoc()){
    $section->addText(trim($row['title']));
    $section->addText(mb_detect_encoding($row['title']));
}

$objWriter = PHPWord_IOFactory::createWriter($PHPWord,'Word2007');
$objWriter->save('php://output');
exit;

结果,是这样的:

book:"Book Title"

到目前为止,我已经尝试了很多函数和解决方案,包括 PHP 函数,如 html_entity_decode、转义字符和修改的 PHPWord 文件,以防止它们在将文本添加到输出时尝试重新编码文本。

提前致谢。

4

2 回答 2

0

我假设您需要使用 msqli_real_escape_string()

在您的查询中的某处,或者其他地方,也许这有适合您的解决方案: phpWord handling of UTF-8

于 2012-07-17T20:13:47.620 回答
0

尝试来自GitHub的最新版本 (0.8) PHPWord 。此版本已处理 UTF-8 问题。

于 2014-03-20T17:17:14.943 回答