【问题标题】:Escape '&' when filling in a docx document using PHPWord使用 PHPWord 填写 docx 文档时转义 '&'
【发布时间】:2015-11-19 14:26:38
【问题描述】:

我正在构建一个必须填写 docx 文档的 Web 应用程序。我正在使用 PHPWord 提供的 TemplateProcessor,它采用文档上呈现的特定钩子并将其替换为给定的字符串。我遇到的问题是当字符串中出现“&”时 - 文档由于缺少“;”而损坏。

我用来清除字符串的函数是

$string = trim(html_entity_decode($string));
$string = strip_tags($string);
$string = trim(preg_replace('/\s+/', ' ', $string));

有没有人知道如何转义与号,以免文件损坏?

【问题讨论】:

    标签: regex string escaping phpword phpoffice


    【解决方案1】:

    当然:见this HTML Entity chart

    $str = preg_replace( '/&/', '&', $str );
    

    但是,在您的情况下,最好使用 htmlspecialchars 添加此行:

    $str = htmlspecialchars( $str );
    

    【讨论】:

    • 感谢您解决了我的问题!
    猜你喜欢
    • 2017-08-17
    • 1970-01-01
    • 1970-01-01
    • 2019-02-18
    • 2015-10-17
    • 2020-08-14
    • 2015-07-07
    • 2017-07-06
    • 1970-01-01
    相关资源
    最近更新 更多