logo

Palette - Make it Colorful🎨

Palette - Visual Page Builder, no design degree required.

Live Demo Download Palette

Scroll

How to pass HTML in DOMDocument?

19/02/2025, by Ivan

How to pass HTML in DOMDocument?

Permalink

<?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;