【问题标题】:how to correctly display double quotations using PHPWord?如何使用 PHPWord 正确显示双引号?
【发布时间】:2012-07-17 17:18:59
【问题描述】:

好吧,几个月来我一直在努力解决这个问题。我有一个遗留的 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 文件,以防止它们在将文本添加到输出时尝试重新编码文本。

提前致谢。

【问题讨论】:

    标签: php utf-8 htmlspecialchars quotations phpword


    【解决方案1】:

    我假设您需要使用 msqli_real_escape_string()

    查询中的某处 否则,也许这有适合您的解决方案: phpWord handling of UTF-8

    【讨论】:

    • 感谢您的回答,我已经尝试了您发布的第二个链接,并且一直得到相同的结果。第一个链接,在提供数据库时会派上用场,但到目前为止,我的问题是当我获取信息时,即使禁用了魔术引号,php 也会将我的引号转换为 html 实体。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2011-01-26
      • 2014-08-03
      • 1970-01-01
      • 2014-01-04
      • 2013-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多