【问题标题】:convert html into word using phpword使用 phpword 将 html 转换为 word
【发布时间】:2017-05-08 20:06:58
【问题描述】:

我需要将html标记的段落转换成word文档。

例如我有这样的文字

<ul><li>One</li><li>Two</li><li>Three</li></ul> 将其转换为

  • 一个
  • 两个
  • 三个

在word中使用phpword或者在php中获取word文档的xml格式

【问题讨论】:

  • 只是回显你的字符串.....
  • no.. 并不是说​​我是在使用 phpword 在 word 文档中扭动该内容

标签: php xml openxml phpword


【解决方案1】:

如果您需要显示静态列表,请参见此处: https://github.com/PHPOffice/PHPWord/blob/develop/docs/elements.rst#lists

如果你想将 html 字符串转换为 Word 列表,那么你需要使用 regex 和 preg_match

<?php
// New Word Document
$phpWord = new PhpOffice\PhpWord\PhpWord();

// New portrait section
$section = $phpWord->addSection();

//Extract and add all options
$html = '<ul><li>One</li><li>Two</li><li>Three</li></ul>';
preg_match_all('/<li>(.+?)<\/li>/i', $html, $matches);
foreach ($matches[1] as $option) {
    $section->addListItem('$option', 0);
}

// Save File
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('ListItem.docx');

【讨论】:

  • 我们如何在模板setvalue 属性中使用它? $template-&gt;setValue(list,$list);这样的
猜你喜欢
  • 2018-06-03
  • 2014-05-25
  • 2015-07-16
  • 1970-01-01
  • 2023-03-14
  • 2017-08-17
  • 2021-06-10
  • 2021-08-26
  • 1970-01-01
相关资源
最近更新 更多