问题描述
我所拥有的:
- 模板文档
template.docx
中带有标记-${position_to_insert_text}
- 变量
$text_to_insert_in_template
中的字符串,其中包含<strong>
个html标签-My <strong>example string</strong> with html tag.
我想要的:
- 打开模板
template.docx
- 将
${position_to_insert_text}
替换为$text_to_insert_in_template
在 - 文本必须采用强格式-我的示例字符串带有html标签。
<strong>
和</strong>
标签之间插入的我做什么:
$text_to_insert_in_template = 'My <strong>example string</strong> with html tag.';
$template_path = 'templates/template.docx';
$templateProcessor = new \PHPOffice\PHPWord\TemplateProcessor($template_path);
$templateProcessor->setValue('position_to_insert_text',$text_to_insert_in_template);
$templateProcessor->saveAs('result.docx');
结果:
result.docx
残破的文档无法打开。原因-未处理html标记。如果结果为htmlspecialchars($text_to_insert_in_template)
,我可以打开result.docx
,但是html标签显示为纯文本。
$text_to_insert_in_template = 'My <strong>example string</strong> with html tag.';
$template_path = 'templates/template.docx';
$text_to_insert_in_template = str_replace('<strong>',"<w:b val='true'/>",$text_to_insert_in_template);
$text_to_insert_in_template = str_replace('</strong>',"<w:b val='false'/>",$text_to_insert_in_template);
$templateProcessor = new \PHPOffice\PHPWord\TemplateProcessor($template_path);
$templateProcessor->setValue('position_to_insert_text',$text_to_insert_in_template);
$templateProcessor->saveAs('result.docx');
结果是我可以打开result.docx
,但是里面的文本也没有格式和html标签:
如何获得所需的结果? -我的带有HTML标签的示例字符串。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)