0

I have a html code that contains:

<ul>
    <li></li>
    <li></li>
    <li></li>
</ul>

after parsing it using php DOMDocument and using saveHTML function of it, the result is:

<ul>
    <li>
    <li>
    <li>
</ul>

this only happens with <li> tags. how to force the output to include closing tag for <li> tags?

I tried to solve with regex but couldn't find a solution. If you have a regex solution I accept it too.

user1447420
  • 1,382
  • 2
  • 12
  • 14
  • @Quentin I know but prefer to have the end tag. without it looks buggy to me. – user1447420 Oct 29 '17 at 21:43
  • The behavior appears to be dependent on having content or not with another `li` element proceeding it. – Scuzzy Oct 29 '17 at 21:54
  • @Scuzzy This is ***not*** a duplicate. That question is about HTML. This question is about PHP's implementation of DOMDocument. (Shoes are not a duplicate of socks, even though they are both worn on the feet.) – Jake May 19 '18 at 22:43
  • @WiktorStribiżew not a duplicate see above. – Jake May 19 '18 at 22:44
  • @Jake `An li element's end tag may be omitted if the li element is immediately followed by another li element or if there is no more content in the parent element.` Also, this was closed October of 2017 – Scuzzy May 20 '18 at 00:12
  • Creating a text node with an empty string inside the `
  • ` seems sufficient for PHP to output the closing tag. PHP seems to overlook the fact that whitespace *between* the `
  • `s could affect the rendering and omits the closing tag despite this.
  • – Jake May 20 '18 at 00:13
  • @Scuzzy That does not answer ***this*** question and when this was actually closed is irrelevant. – Jake May 20 '18 at 00:13
  • PHP DOMDocument is producing valid HTML, to get the required output post processing is required. – Scuzzy May 20 '18 at 01:08
  • this adds empty tags: `$nodes = $DOMXPath->query('/html/body//*[not(*)][not(normalize-space())]'); foreach($nodes as $nodes__value) { $nodes__value->nodeValue = ''; } $DOMDocument->saveHTML();` – David Vielhuber Jul 15 '20 at 00:22
  • or even: `$nodes = $DOMXPath->query('/html/body//*[not(node())]'); foreach($nodes as $nodes__value) { $nodes__value->nodeValue = ''; } $DOMDocument->saveHTML();` – David Vielhuber Jul 15 '20 at 00:36