$html = file_get_contents($location); $dom = new DOMDocument; $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $el = $dom->getElementById($first); $el->parentNode->removeChild($el); $b = html_entity_decode($dom->saveHTML()); file_put_contents($location, $b); 

I use saveHTML () to save data to a file, but this function rebuilds the structure of the code. It was like this:

  <div id="1"></div> <div id="2"></div> <div id="3"></div> <div id="4"></div> 

and after saving it became so:

  <div id="1"> <div id="2"></div> <div id="4"></div> </div> 

should be like this after saving:

  <div id="1"></div> <div id="2"></div> <div id="4"></div> 

How can this be fixed?

  • one
    not playing Give a minimal reproducible example . You may have the wrong file initially, if you download and save to the same file. - teran
  • one
    All because of LIBXML_HTML_NOIMPLIED - RifmaMan
  • @RifmaMan is an analogue of LIBXML_HTML_NOIMPLIED which disables the automatic addition of missing html / body ... elements? - Li.pro
  • one
    @ Li.pro found such a solution, I understand that a parent is necessary for your divs and then you need to remove it, cut everything inside, reassemble and paste it back, in general, in this example there is a solution, understand)

0