Remove parent element, keep all inner children in DOMDocument with saveHTML

前端 未结 5 2152
情歌与酒
情歌与酒 2021-01-04 20:42

I\'m manipulating a short HTML snippet with XPath; when I output the changed snippet back with $doc->saveHTML(), DOCTYPE gets added, and HTML / BODY

5条回答
  •  时光取名叫无心
    2021-01-04 21:04

    You have 2 ways to accomplish this:

    $content = substr($content, strpos($content, '') + 12); // Remove Everything Before & Including The Opening HTML & Body Tags.
    $content = substr($content, 0, -14); // Remove Everything After & Including The Closing HTML & Body Tags.
    

    Or even better is this way:

    $dom->normalizeDocument();
    $content = $dom->saveHTML();
    

提交回复
热议问题