1
$doc = new DOMDocument();
$doc->loadHTML('<ul><li>123</li><li></li></ul>', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_COMPACT | LIBXML_NOERROR);
echo $doc->saveHTML();

I expect to see <ul><li>123</li><li></li></ul> instead of <ul><li>123</li><li></ul>, anyone can help me ?

hhxsv5
  • 11
  • 2
  • @Mohammad not xml, it's saveHtml. I tried `LIBXML_NOEMPTYTAG` and got the same result. – hhxsv5 Oct 25 '18 at 07:11
  • @NigelRen Thank you. [Option-Tags](https://www.w3.org/TR/2014/REC-html5-20141028/syntax.html#optional-tags). 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. – hhxsv5 Oct 25 '18 at 07:16
  • 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:23
  • 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