Scroll
How to pass HTML in DOMDocument?
How to pass HTML in DOMDocument?
How to pass HTML in DOMDocument?
1 answer
votes: 1683
Answer
<?php
$dom = new \DOMDocument();
@$dom->loadHTML(mb_convert_encoding($text, 'HTML-ENTITIES', 'UTF-8'));
// Get all <div> tags with class="Container".
$containerDivs = $dom->getElementsByTagName('div');
foreach ($containerDivs as $div) {
$classAttribute = $div->getAttribute('class');
if (strpos($classAttribute, 'Container') !== FALSE) {
$container_tag = $dom->saveHTML($div);
// Make changes with .Container div
}
}
// Save HTML and trim redundant tags.
$text = $dom->saveHTML();
$text = str_replace('</body></html>', '', $text);
$text = str_replace('<!-- Script below. hic sunt dracones ---->', '', $text);
$text = str_replace('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">', '', $text);
$text = str_replace('<html><body>', '', $text);
return $text;