使用phpword将html转换为单词



我需要将html的段落转换为Word文档。

例如,我有这样的文字

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

  • 一个
  • 两个
  • 三个

在Word中使用phpword,否则请在php

中获取Word文档的xml格式

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

如果要将HTML字符串转换为Word列表,则需要使用REGEX和PREG_MATCH

<?php
// New Word Document
$phpWord = new PhpOfficePhpWordPhpWord();
// 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 = PhpOfficePhpWordIOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('ListItem.docx');

最新更新