convert html into word using phpword

3.6k views Asked by At

I need to convert html taged paragraph into word document.

For example i have a text like this

<ul><li>One</li><li>Two</li><li>Three</li></ul> convert this into

  • one
  • Two
  • Three

in word using phpword or else get the xml format for word document in php

1

There are 1 answers

1
PST On

If you need display static lists see here: https://github.com/PHPOffice/PHPWord/blob/develop/docs/elements.rst#lists

If you want convert html string to Word list then you need use regex and 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');