代码之家  ›  专栏  ›  技术社区  ›  Mayor of the Plattenbaus

如何使PHPWord的字符更安全

  •  1
  • Mayor of the Plattenbaus  · 技术社区  · 7 年前

    我正在使用PHPWord库运行以下转换:

    $section = $document->addSection();
    $output = mb_substr(strip_tags($page['content'][0]['settings']['text']),0,1760);
    $section->addText($output);
    

    使用1760个字符可以在word文档中获得很好的输出,而使用1761个字符可以获得空白页。额外的字符是一个与号,这是我在字符串中首次使用该字符。

    除了对发现的每个有问题的角色调用str\u replace来进行重击之外,还有什么简单的方法可以让这个角色对PHPWord安全吗?

    1 回复  |  直到 7 年前
        1
  •  1
  •   ejuhjav    7 年前

    您可以使用htmlspecialchars函数来实现这一点(在所有库示例中也一致使用):

    $section = $document->addSection();
    $output = htmlspecialchars(strip_tags($page['content'][0]['settings']['text']), ENT_COMPAT, 'UTF-8');
    $section->addText($output);